ADDITIONAL SYSTEM INFORMATION :
Linux 64 bit, Windows 10 64 bit
A DESCRIPTION OF THE PROBLEM :
Value returned by java.lang.management.MemoryUsage.getUsed() for eden space usage does not match with the value shown by jcmd <pid> GC.heap_info command. Following code always shows eden space usage as 0.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Following code prints the memory usage on console. Once the program prints memory usage, put it in background and capture the jcmd output using "jcmd <pid> GC.heap_info" command. Compare the eden space usage values between these two approaches.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Eden space usage as reported by java.lang.management.MemoryUsage should be close to the value reported by jcmd output.
ACTUAL -
Eden space usage reported by java.lang.management.MemoryUsage is always 0.
---------- BEGIN SOURCE ----------
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.HashSet;
public class HeapUsageTest {
public HeapUsageTest()
{
System.out.println("Before ");
printHeapUsage();
HashSet<String> set = new HashSet<>();
for (int i = 0; i<10000000; i++) {
set.add(String.valueOf(i));
}
System.out.println("After");
printHeapUsage();
}
public static void main(String[] args) throws InterruptedException {
new HeapUsageTest();
Thread.sleep(1000000);
}
private void printHeapUsage() {
for (MemoryPoolMXBean mpbean :
ManagementFactory.getMemoryPoolMXBeans()) {
MemoryUsage usage = mpbean.getCollectionUsage();
if (usage != null) {
long max = usage.getMax();
long used = usage.getUsed();
System.out.println(mpbean.getName() + " Used " + used + ", Max " + max);
}
}
}
}
---------- END SOURCE ----------
FREQUENCY : always