Name | Modified | Size | Downloads / Week |
---|---|---|---|
jmemoryuse-example.jar | 2010-05-09 | 3.2 kB | |
README.txt | 2010-05-09 | 1.8 kB | |
jmemoryuse-src.jar | 2010-05-09 | 9.5 kB | |
jmemoryuse.jar | 2010-05-09 | 3.4 kB | |
Totals: 4 Items | 17.9 kB | 0 |
The purpose of this project is to provide a simple and efficient way to correctly calculate the memory use of one or more Java objects. In order to do this in a portable way on multiple JVMs and JVM versions the instrumentation support is used. The MemoryUse class needs to be installed as a "agent" using the "-javaagent:" jvm option. This also makes the class available for your application (the class gets loaded by the system class loader). The instrumentation support only provides a method for calculating the memory used by a specified object itself and not the objects referenced by its fields (or in the case of a non primitive array by the array elements). The class contain logic for also adding in the memory used by these referenced objects if so desired (deep = true). In order to make it possible to make use of the memory usage not only for educational or profiling purposes but also as part of an application (for instance to implement caches that occupy a max amount of memory rather than a maximum number of objects) the class makes use of several internal optimizations. The most important are caching of the instance size (for classes with only primitive fields) or the fields that needs to be checked for other classes. In order to not prevent unloading of unused classes (and to remove cached fields etc for unloaded classes) the internal caching uses a WeakHashMap. This project is preferably used by downloading the com.example.jmemoryuse.jar file. If you decide to build it from source use the provided Ant file. To run the example program execute: "java -javaagent:.\jmemoryuse.jar -jar .\jmemoryuse-example.jar" or check out the "run target" in the build.xml file! I hope this simple project will be of use for others - I have sure had a lot of use for it so far! /JavaFanBoy