Why do I get the java error "Could not reserve enough space for object heap"?

When running an RCE Powered Application -- for example an RCE Shell -- you have the opportunity to reserve a requested amount of system memory.

The Java JRE is not aware of the resource limit you have set, and will try (and fail) to grab as much of the system memory as is free. (Java bug 8043516)

This will typically result in one of the following errors:

"Could not reserve enough space for object heap"

"Could not allocate metaspace"

"Unable to allocate bitmaps for parallel garbage collection for the requested heap"

To correct this behavior, you can set the shell environment variables JAVA_OPTS and _JAVA_OPTIONS before launching your java application. These values must be set within the shell that launches your java application (i.e. the parent process). You can set these values and run your java app from within a single interactive RCE Powered Shell. Or, for a Batch job, you can create a shell script that sets these values first and then afterward runs your java app.

  • To fix the error "Could not reserve enough space for object heap", add the options "-Xmx<size>m" to set the maximum size for the object heap memory allocation. This must be set large enough to accommodate loading your application into memory, but smaller than your requested total memory allocation by 2GB.
  • To fix the error "Could not allocate metaspace", add the options "-XX:CompressedClassSpaceSize=<size>m" to set the size of the class metadata memory allocation. In testing, 256m has been sufficient.
  • To fix the error "Unable to allocate bitmaps for parallel garbage collection for the requested heap", you may either reduce one of the memory allocations litsted above (heap, or compressed class metadata) -- or you can turn off parallel garbage collection to save memory at the cost of some performance by adding the option "-XX:+UseSerialGC".

For example, to restrict the Java JRE heap memory allocation to a maximum size of 2GB and the class metadata to a size of 256MB, to run within a 4GB memory reservation, set the variables as follows:

in bash shell:

export JAVA_OPTS="-Xmx2048m -XX:CompressedClassSpaceSize=256m"
export _JAVA_OPTIONS="-Xmx2048m -XX:CompressedClassSpaceSize=256m"

in tcsh shell:

setenv JAVA_OPTS "-Xmx2048m -XX:CompressedClassSpaceSize=256m"
setenv _JAVA_OPTIONS "-Xmx2048m -XX:CompressedClassSpaceSize=256m"

A similar problem results from the Java JRE not correctly detecting the number of CPUs available. To specify the number of CPUs for Java to use, add to the above commands: " -XX:ActiveProcessorCount=1" (where "1" is the number of CPUs you have reserved for your job.

For more information see: