Organizational Research By

Surprising Reserch Topic

Question:Discrepancy between memory usage got through getMemoryMXBean() and jvisualvm?


When trying to monitor my own program's memory usage through the following code

public static String heapMemUsage()
{
    long used = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
    long max = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
    return ""+used+" of "+max+" ("+ used/(double)max*100.0 + "%)";
}

I got a slightly different result than seen through jvisualvm (17 588 616 in my program vs 18 639 640 in jvisualvm). I know it's not that big of a deal, but it did get me thinking.

Is there any explanation for this fact?

I'd like to use the coded version if possible, but if its results are in some way skewed, being jvisualvm in some way more credible, I'll have to stick with jvisualvm instead.

asked Sep 13, 2013 in ANDROID by android_master
edited Sep 12, 2013
0 votes
17 views



Related Hot Questions

2 Answers

0 votes

VisualVM does use the same approach to get required values, let's check the source:

MonitoredDataImpl(Jvm vm,JmxSupport jmxSupport,JvmMXBeans jmxModel) {
    //...
    MemoryUsage mem = jmxModel.getMemoryMXBean().getHeapMemoryUsage();
    MemoryPoolMXBean permBean = jmxSupport.getPermGenPool();    
    //...
    genCapacity = new long[2];
    genUsed = new long[2];
    genMaxCapacity = new long[2];
    genCapacity[0] = mem.getCommitted();
    genUsed[0] = mem.getUsed();

    genMaxCapacity[0] = mem.getMax();

    if (permBean != null) {
        MemoryUsage perm = permBean.getUsage();
        genCapacity[1] = perm.getCommitted();
        genUsed[1] = perm.getUsed();
        genMaxCapacity[1] = perm.getMax();
    }
  }

So it is safe to use your approach anyway. Please post additional info regarding JVM version, etc in order to trace this issue.

 

answered Sep 13, 2013 by android_master
edited Sep 12, 2013
0 votes

VisualVM does use the same approach to get required values, let's check the source:

MonitoredDataImpl(Jvm vm,JmxSupport jmxSupport,JvmMXBeans jmxModel) {
    //...
    MemoryUsage mem = jmxModel.getMemoryMXBean().getHeapMemoryUsage();
    MemoryPoolMXBean permBean = jmxSupport.getPermGenPool();    
    //...
    genCapacity = new long[2];
    genUsed = new long[2];
    genMaxCapacity = new long[2];
    genCapacity[0] = mem.getCommitted();
    genUsed[0] = mem.getUsed();

    genMaxCapacity[0] = mem.getMax();

    if (permBean != null) {
        MemoryUsage perm = permBean.getUsage();
        genCapacity[1] = perm.getCommitted();
        genUsed[1] = perm.getUsed();
        genMaxCapacity[1] = perm.getMax();
    }
  }

So it is safe to use your approach anyway. Please post additional info regarding JVM version, etc in order to trace this issue.

 

answered Sep 13, 2013 by android_master
edited Sep 12, 2013

...