How to control Java heap size (memory) allocation (xmx, xms)

Java memory FAQ: How do I control the amount of memory my Java program uses (i.e., Java RAM usage)?

One of the Linux servers that I use is a little starved for memory, but I need to run a Java program on it periodically to run some utility tasks. However, every time I try to run the program I get this Java heap size error message:

“Error occurred during initialization of VM. Could not reserve enough space for object heap. Could not create the Java virtual machine.”

I knew my program didn’t really need that much memory — it was just hitting a database and generating some files and reports — so I got around this memory limit problem by specifying the maximum Java heap size my program was allowed to allocate. In my case I didn’t think about it too hard and just chose a heap size limit of 64 MB RAM, and after I set this RAM limit my program ran fine.

Back to top

Setting the maximum Java heap size (Xmx)

You set the maximum Java heap size of your program using the -Xmx option to the Java interpreter. To specifically limit your heap size to 64 MB the option should be specified like this:


Using that memory limit setting, the Java command I use in my shell script to start my Java program looks like this:

java -Xmx64m -classpath ".:${THE_CLASSPATH}" ${PROGRAM_NAME}

where THE_CLASSPATH and PROGRAM_NAME are variables set earlier in my script. (The important part here is the -Xmx64m portion of the command.)

Back to top

You can find more options for controlling Java application memory use by looking at the output of the java -X command. Here's what the output of those commands looks like from my JVM:

$ java -X

-Xmixed           mixed mode execution (default)
-Xint             interpreted mode execution only
-Xbootclasspath:  set search path for bootstrap classes and resources -Xbootclasspath/a:                   append to end of bootstrap class path -Xbootclasspath/p:                   prepend in front of bootstrap class path -Xnoclassgc       disable class garbage collection -Xloggc:    log GC status to a file with time stamps
-Xbatch           disable background compilation
-Xms              set initial Java heap size
-Xmx              set maximum Java heap size
-Xss              set java thread stack size
-Xprof            output cpu profiling data
-Xfuture          enable strictest checks, anticipating future default
-Xrs              reduce use of OS signals by Java/VM (see documentation)
-Xdock:name=      override default application name displayed in dock -Xdock:icon=                   override default icon displayed in dock -Xcheck:jni       perform additional checks for JNI functions -Xshare:off	      do not attempt to use shared class data -Xshare:auto      use shared class data if possible (default) -Xshare:on	      require using shared class data, otherwise fail.  The -X options are non-standard and subject to change without notice. 

From that list, the command-line arguments specifically related to Java application memory use are:

-Xnoclassgc   disable class garbage collection
-Xms          set initial Java heap size
-Xmx          set maximum Java heap size
-Xss          set java thread stack size
Back to top

Java heap size descriptions (xms, xmx, xmn)

Digging around, I just found this additional Java xms, xmx, and xmn information on Apple's web site:

-Xms size in bytes
Sets the initial size of the Java heap. 
The default size is 2097152 (2MB). 
The values must be a multiple of, and greater than, 1024 bytes (1KB).
(The -server flag increases the default size to 32M.)

-Xmn size in bytes
Sets the initial Java heap size for the Eden generation. 
The default value is 640K. 
(The -server flag increases the default size to 2M.)

-Xmx size in bytes
Sets the maximum size to which the Java heap can grow. 
The default size is 64M. 
(The -server flag increases the default size to 128M.) 
The maximum heap limit is about 2 GB (2048MB).
Back to top

Java memory arguments (xms, xmx, xmn) formatting

When setting the Java heap size, you should specify your memory argument using one of the letters “m” or “M” for MB, or “g” or “G” for GB. Your setting won’t work if you specify “MB” or “GB.” Valid arguments look like this:

  • -Xms64m or -Xms64M
  • -Xmx1g or -Xmx1G

Also, make sure you just use whole numbers when specifying your arguments. Using -Xmx512m is a valid option, but -Xmx0.5g will cause an error.

Back to top

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.