How do I set Java's min and max heap size through environment variables?
I know that the heap sizes can be set when launching java, but I would like to have this adjusted through environment variables on my server.
How do I set Java's min and max heap size through environment variables?
I know that the heap sizes can be set when launching java, but I would like to have this adjusted through environment variables on my server.
You can't do it using environment variables. It's done via "non standard" options. Run: java -X
for details. The options you're looking for are -Xmx
and -Xms
(this is "initial" heap size, so probably what you're looking for.)
I think your only option is to wrap java in a script that substitutes the environment variables into the command line
You can't do it using environment variables directly. You need to use the set of "non standard" options that are passed to the java command. Run: java -X for details. The options you're looking for are -Xmx and -Xms (this is "initial" heap size, so probably what you're looking for.)
Some products like Ant or Tomcat might come with a batch script that looks for the JAVA_OPTS environment variable, but it's not part of the Java runtime. If you are using one of those products, you may be able to set the variable like:
set JAVA_OPTS="-Xms128m -Xmx256m"
You can also take this approach with your own command line like:
set JAVA_OPTS="-Xms128m -Xmx256m"
java ${JAVA_OPTS} MyClass
Is there a max. size you can set Xmx to? I set it to 1024m and eclipse opens ok. When I set it above 1024, eclipse doesn't open and I get the error "jvm terminated. Exit code=-1"... I was doing this because I keep getting an "java.lang.OutOfMemoryError: Java heap space". I am reading in a 35.5Mb .txt file and this error occurs when it's just reading in the file using the "while((line = reader.readLine()) != null)" loop. I would have thought that 1024mb would have been enough. Can anyone help me?
A couple of notes:
Apache ant doesn't know anything about JAVA_OPTS, while Tomcat's startup scripts do. For Apache ant, use ANT_OPTS to affect the environment for the JVM that runs /ant/, but not for the things that ant might launch.
The maximum heap size you can set depends entirely on the environment: for most 32-bit systems, the maximum amount of heap space you can request, regardless of available memory, is in the 2GiB range. The largest heap on a 64-bit system is "really big". Also, you are practically limited by physical memory as well, since the heap is managed by the JVM and you don't want a lot of swapping going on to the disk.
For server environments, you typically want to set -Xms and -Xmx to the same value: this will fix the size of the heap at a certain size and the garbage collector has less work to do because the heap will never have to be re-sized.