I have to calculate the memory consumed by my Java program. I have used following line in the program to calculate the memory
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); MemoryUsage usage = mbean.getHeapMemoryUsage(); MemoryUsage nonHeapusage = mbean.getNonHeapMemoryUsage(); long total = (usage.getUsed()+nonHeapusage.getUsed())/1024;
That is displaying the memory consumed in KB. But when I look at java.exe process in Task Manager on Windows machine, that was showing more memory consumed (approx 9MB more) than what is displayed by my calculation.
I have some questions on that: 1.Is the memory being consumed by some other mechanism other than heap and non heap? 2.Is there any other way to calculate the memory same as task manager? 3.What is this NonHeapMemoryUsage, is this same as stack? 4.The MemoryMXBean is returning the pool of memory by calling ManagementFactory.getMemoryPoolMXBeans(). What are those pools and whether they plays a role in memory consumption? I have once added the memory usage by all the pools. That size is much bigger that Task Manager size. And is growing continuously.
The column "Memory Usage", displayed by default, is the amount of physical memory used. This is not a figure that would be expected to correlate with Java's reported memory usage.
The column "VM Size", not displayed by default but selectable in View menu, shows the amount of virtual memory used. This should correlate with Java's reported memory usage. Of course, the Task Manager reports virtual memory usage of the whole Java process, including JVM's internal memory requirements, so while the figures are strongly correlated, they may not be equal.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Jun 14, 2007
Thanks for your reply
I have found that the virtual memory usage 2 MB more than the Physical memory usage. That was again 10 MB more than what I have calculated (Heap + Non Heap memory) using MemoryMXBean. I am unable to correlate with the Virtual Memory and Physical memory in Task manager
run a JVM, load nothing but a test class that checks the memory usage, see how that compares, there's likely some non-reported memory being used by the JVM,
Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Joined: Jun 14, 2007
I have tried the same. Still the way I have calculated the memory is showing different usage then what was displayed in Task Manager. About 10MB difference with Virtual memory. I want to know is there any other way to calculate the same.