Java: Difference between revisions

Jump to navigation Jump to search
449 bytes added ,  6 years ago
no edit summary
No edit summary
No edit summary
Line 38: Line 38:


===Memory Issues=== <!--T:8-->
===Memory Issues=== <!--T:8-->
Java uses an automatic system called ''garbage collection'' to identify variables which are out of scope and return the memory associated with them to the operating system which however doesn't stop many Java programs from requiring significant amounts of memory to run correctly. When a Java virtual machine is launched using the <tt>java</tt> command by default the initial and maximum heap size are set to 1/64 and 1/4 of the system's physical memory respectively. This amount, particularly the maximum heap size, may well be inadequate and leaves a substantial amount of physical memory unused. To correct this problem, you can tell the Java virtual machine the maximum amount of memory to use with the command line argument <tt>Xmx</tt>, for instance
The Java virtual machine expects to have access to all the physical memory on a given compute node, while the scheduler or a shell might impose its own memory limits (often different) according to the submission script specifications or limitations of the login node. In a shared computing environment, these limits ensure that finite resources, such as memory and CPU cores, do not get exhausted by one job at the expense of another.
{{Command|java -Xmx8192m -jar file.jar}} 
 
tells the Java virtual machine that it can use up to 8192 MB (8 GB) of memory. You can set the initial heap size with the argument <tt>Xms</tt> and you can see all the command line options the JVM is going to run with by specifying the following flag <tt>-XX:+PrintCommandLineFlags</tt>.
When the Java VM starts, it sets two memory parameters according to the amount of physical rather than available memory as follows:
* Initial heap size of 1/64 of physical memory
* Maximum heap size of 1/4 of physical memory
 
These two parameters can be explicitly controlled on the command line by following either syntax below:
  java -Xms256m -Xmx4g -version
or
  java -XX:InitialHeapSize=256m -XX:MaxHeapSize=4g -version
 
On a system with a lot of physical memory, the 1/4 can easily exceed the memory limits imposed by a shell or by the scheduler, and Java will fail with messages like these
 
  Could not reserve enough space for object heap
  There is insufficient memory for the Java Runtime Environment to continue.
 
You can see all the command line options the JVM is going to run with by specifying the following flag <code>-XX:+PrintCommandLineFlags</code>, like so:
<pre>
$ java -Xms256m -Xmx4g -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:ParallelGCThreads=4 -XX:+PrintCommandLineFlags -XX:+UseCompressedOops -XX:+UseParallelGC
</pre>


<!--T:9-->
<!--T:9-->
cc_staff
318

edits

Navigation menu