If your Java program requires a large amount of memory, it’s possible that the virtual machine will begin to throw OutOfMemoryError instances when attempting to instantiate objects. In some cases, this may be the result of a programming error, but in others, it’s simply a result of your program legitimately using more memory than is available. In this latter case, you can increase the “heap size” allocated by the Java Virtual Machine (JVM) by using command line options. When not specified, the heap size defaults to 1 MB, and can increase to as much as 16 MB if your program requires more memory. To set the initial amount of memory allocated for your program, use the -ms option with a 1.1 JVM, and the -Xms option with a 1.2 (also known as Java 2) JVM. To set the maximum amount of memory that can be allocated for your program, use the -mx or -Xmx for Java 1.1 or 1.2, respectively. For example:
java -ms32m -mx128m MyClassName (Java 1.1)java -Xms32m -Xmx128m MyClassName (Java 1.2 / 2.0)
In each case, the options specify that 32 MB of memory should be allocated initially for the program to run in, and that up to 128 MB may be allocated if necessary.
You should note the presence of the “X” on the Java 1.2 / 2.0 options. It indicates that these are non-standard options that might not function the same way or could even be removed in future JVMs. In the meantime, you may find it necessary to take advantage of these options if your program requires a large amount of memory.