aspose file tools*
The moose likes Java in General and the fly likes Memory calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory calculation" Watch "Memory calculation" New topic

Memory calculation

Amit Arora
Ranch Hand

Joined: Jun 14, 2007
Posts: 30

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.

Amit Arora

Amit Arora<br />SCJP 1.4, SCWCD
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
In Task Manager, which column are you looking at?

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.
Amit Arora
Ranch Hand

Joined: Jun 14, 2007
Posts: 30
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
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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 -
if (Posts < 30) JavaRanchFAQ);
Amit Arora
Ranch Hand

Joined: Jun 14, 2007
Posts: 30
Hi Bill,

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.

Thanks and Regards,
Don't get me started about those stupid light bulbs.
subject: Memory calculation