This section provides a tutorial example on how to use the '-X' option for the 'java' tool to specify non-standard options. The tutorial example shows how to control the memory size of the JVM.
"java" can also take "non-standard" options by using the "-X" option.
Two of the "non-standard" options are very useful to control the memory size of the JVM to be launched.
-Xmsn
-Xmxn
where "ms" specifies the initial size of the JVM, "mx" specifies the maximum size of the JVM, and "n"
specifies the memory size in bytes, kilo bytes or mega bytes. For example: -Xms32M and -Xmx64M.
Here is a program to show you how to use these options:
/**
* ShowMemory.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
class ShowMemory {
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);
}
}
I launched the program with different options:
C:\herong>javac ShowMemory.java
C:\herong>java ShowMemory
Free memory: 1896232
Total memory: 2031616
C:\herong>java -Xms32M -Xmx128M ShowMemory
Free memory: 33222440
Total memory: 33357824
C:\herong>java -Xms64M -Xmx128M ShowMemory
Free memory: 66514728
Total memory: 66650112
Note that ShowMemory is running an infinite loop at the end. You need to stop the program by "Ctrl-C"