Problem category: Runtime Error
Diagnosis Difficulty: Medium-Hard
Difficulty to Fix: Easy
The JVM's default memory allocation is typically enough for a simple program. However, larger programs with memory intensive computation may result in throwing a java.lang.OutOfMemoryError exception during the execution of a program. A more fundamental analysis of memory usage and object allocation should be performed to confirm that the problem isn't an issue with the program design (such as keeping references to non-needed object), otherwise setting the maximum heap size may just cover up a more fundamental problem.
Suggested fixes
Increase the java virtual machine's default memory using the -Xmx option. By default, the maximum memory usage for the JVM is 64M. To increase this value, upon execution of the program pass the JVM the -Xmx* option, where * equals the requested memory (typically divisible by 64 until it reaches the gigabyte stage). For example, to run the program from the command line, call:
java -Xmx512M myprogram
One can increase this further (for example -Xmx1G) depending upon the available memory on the computer launching the program. For those using an IDE such as Eclipse, one should be able to pass the JVM arguments automatically. In Eclipse, go Run->Run Configurations->Arguments and enter the -Xmx option into the VM Arguments area.
The problem gets a bit more difficult for deployment. Specifying the maximum memory for an executable jar is not possible without platform specific options (batch files, application bundles, etc...), however deploying applications via Java webstart allows the memory to be set via the max-heap-size parameter.
Note: One can also set the minimum size of the JVM using the -Xms option in a similar way.