• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memory calculation

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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.

Regards,
Amit Arora
SCJP,SCWCD
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amit Arora
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Amit Arora
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Amit
SCJP and SCWCD
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic