This section provides a tutorial example on how to check HotSpot VM memory usages on Windows systems using the Runtime class.
Let's use the following simple program to see the memory usages of the Java
HotSpot VM:
class LongWhile {
public static void main(String[] a) {
Runtime rt = Runtime.getRuntime();
System.out.println(" Free memory: " + rt.freeMemory());
System.out.println("Total memory: " + rt.totalMemory());
while (true);
}
}
The following notes were taken in 2007 on a Windows XP system.
This program was compiled with JDK 6u2, and executed in two command windows:
one with the Client VM, and the other with the Server VM. They both reported the
same amount total memory and free memory in the run time environment,
as shown in the table below:
C:\>\progra~1\java\jdk1.6.0_02\bin\java LongWhile
Free memory: 4997088
Total memory: 5177344
C:\>\progra~1\java\jdk1.6.0_02\bin\java -server LongWhile
Free memory: 4997088
Total memory: 5177344
So the JVM itself used about 5177344 - 4997088 = 180256 bytes for both client and server versions.
Comparing with notes taken on JDK 1.4.0, JDK 6 seems to have smaller footprint 252240 - 180256 = 71984 bytes.
While the two programs were running, I also used the Windows XP Task Manager
to look at them from the operating system point of view. The Task Manager reported
a 6K less memory usage in the Server VM. See the table bellow for details:
Task Manager Run Time Environment
JVM Memory CPU Free Total
Client VM 6996K 50% 4997088 5177344
Server VM 6990K 50% 4997088 5177344
The following notes were taken in 2004 on a Windows 2000 system.
This program was compiled with j2sdk1.4.0_02, and executed in two command windows:
one with the Client VM, and the other with the Server VM. They both reported the
same amount total memory in the run time environment. But the Server VM gives 248
bytes less free memory.
While the two programs were running, I also used the Windows 2000 Task Manager
to look at them from the operating system point of view. The Task Manager reported
a 676K more memory usage in the Server VM.
See the table bellow for details:
Task Manager Run Time Environment
JVM Memory CPU Free Total Used
Client VM 4576K 50% 1779376 2031616 252240
Server VM 5252K 50% 1779128 2031616 252488