This is a post summarizing my initial research into reliable ways to measure the heap memory usage for the JVM. Previously, I have used the linux top command and look at resident memory for a rough estimate of the heap usage for JVM process. However, I found that inaccurate and I needed more precise measures to reflect some of the memory saving techniques I employed in my optimized MapReduce runtime. Suggestions from Shams indicated that Java MXBean could be used to see the memory usage. Also, I could try to acquire a heap dump and use Visual VM or other tools to see the heap objects, as put forward by Prof. Mellor-Crummey.
To use MXBeans to monitor the JVM heap utilization, I used the following line of code
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean() ; MemoryUsage heap = memBean.getHeapMemoryUsage(); MemoryUsage nonHeap = memBean.getNonHeapMemoryUsage(); System.err.println(String.format("Heap: Init: %d, Used: %d, Committed: %d, Max: %d", heap.getInit(), heap.getUsed(), heap.getCommitted(), heap.getMax()));
It retries the heap attribute from MxBeans factory. The sub fields, init, used, committed, max represent
- init: related to the minimum amount of heap given, the amount specified by the -Xms option
- used: the amount used by the program, including the live objects and garbage collected objects
- committed: amount of memory that is committed for the JVM to use
- max: maximum amount of memory that can be used by the JVM, related to the -Xmx option.
The official documentation can be found here
MemoryUsage getHeapMemoryUsage()Returns the current memory usage of the heap that is used for object allocation. The heap consists of one or more memory pools. The used and committed size of the returned memory usage is the sum of those values of all heap memory pools whereas the init and max size of the returned memory usage represents the setting of the heap memory which may not be the sum of those of all heap memory pools.The amount of used memory in the returned memory usage is the amount of memory occupied by both live objects and garbage objects that have not been collected, if any.
The mapped type of MemoryUsage is CompositeData with attributes as specified in
MemoryUsageobject representing the heap memory usage.