Reputation: 799
I'm trying to debug why a program which prints Hello
and then Thread.sleep(3600*1000)
uses ~404MB of virtual memory under linux (with both OpenJDK and Sun JVMs) on both CentOS 5 and Ubuntu 10.04 server. (a MediaTemple (dv) and (ve), respectively).
But more specifically, when I connect using VisualVM or JConsole I see what looks like reasonable memory usage: 820KM heap (after GC); 8.4MB PermGen; 1136 classes loaded.
Why the discrepancy?
Is purely having a JVM running using that much memory? I tried starting two Hello apps to see if the memory usage scales linearly, and it does:
I started with 78MB of total RAM used. Starting one hello app bumps the memory usage to 376MB. Starting another hello app gives a total RAM usage of 656MB, so memory doesn't seem to be shared well. (these numbers are not virtual memory
(these specific numbers are with OpenJDK, but using the Sun 6 JDK gives comprable, but slightly lower memory usage)
Code:
public class Hello {
public static void main(String[] args) throws Throwable {
System.out.println("HI");
Thread.sleep(3600 * 1000);
return;
}
}
Command Line:
java -Xmx32m -Xms32m Hello
With JMX:
java -Dcom.sun.management.jmxremote.port=8005 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xmx32m -Xms32m Hello
Thank you for your help,
Wiktor
Upvotes: 2
Views: 2395
Reputation: 37007
The heap is only one of the memory areas that Java uses.
See Section 3.5 in the Java Virtual Machine Specification Overview
The PermGen is another one that you can specify how much Java is allowed to use.
Each thread has a virtual machine stack, which you also limit the size of.
There is a Code Cache, or Method Area to store the compiled code.
There are possibly native method stacks.
Memory mapped files, or portions of files (such as indexes of jar file), take up part of the process's (non heap) memory area.
Upvotes: 3