How to Change Java Heap Space
The Java system takes a lot of worries about memory management off the developers, but it still has to have some memory to work with. This memory is known as the "heap," the space that the operating system gives Java to let it run a particular program. When there's enough space in the heap, the Java program runs fine; when there isn't, the program either runs slowly or crashes with an "OutOfMemoryException" error. The Java environment provides for a default heap size, but you can configure this on a per-application or system wide level.
Things You'll Need
- Java Runtime Environment (JRE) 1.6 or greater (earlier versions may work as well)
Instructions
-
Command-Line (Per Application)
-
1
Open a command window (Windows) or terminal/shell window (Unix/Linux) and navigate to the application you wish to run.
-
2
To indicate an initial heap size, run your program with the following parameters:
java -Xms<size> (your application command line)
For example, to run MyApp.jar with a starting heap size of 64 MB, you would run it like so:
java -Xms64m -jar MyApp.jar
-
-
3
To indicate a maximum heap size, change your command line to the following:
java -Xms<initial size> -Xmx<max size> (application command line)
Assuming we want the example from step two to run with a maximum heap size of 128 MB, use the following command line:
java -Xms64m -Xmx128m -jar MyApp.jar
This will ensure the application starts off with a minimum of 64 MB of heap space allocated for it, and never tries to allocate more than 128 MB.
Control Panel (System Wide/For Applets)
-
4
On Windows operating systems, open your control panel ("Start menu > Control Panel") and find the icon labeled "Java." (This may not be available in earlier versions of Java.)
-
5
In the Java Control Panel application, go to the "Java" tab and click the "View..." button. You'll be presented with a window containing a list of the installed Java Runtime Environments on your system.
-
6
Select your primary (or only) Java environment from the list and double-click in the cell labeled "Runtime Parameters."
-
7
Enter the initial and maximum heap sizes as necessary, using the parameters used in the first section above. For example, to use an initial heap size of 64MB and a maximum heap size of 128MB, fill in the box with the following:
-Xms64m -Xmx128m
-
8
Click "OK" to close the runtime environments window, and "OK" again to close the Java control panel.
-
1
Tips & Warnings
If you're a developer, make sure you've used a profiler or some other memory tracking tool to check your application's memory requirements--it'll make gauging your settings much easier when you have a rough idea, rather than just going by trial and error.
If you're not a developer, remember that most applications have their own memory requirements, and the developers should know these before they release their application to the public. If you're having problems with a program running slowly or running out of memory, check the developer's website to see if they have exact settings for you to use.
Changing the values in the control panel--across the system--may seem easier and faster, but it can be dangerous. Lots of Java applications are developed based on the default memory settings, and playing with these may have unexpected results with other software.
References
- Photo Credit memory image by Valentin Mosichev from Fotolia.com