Java

Notitle

Java is a general-purpose computer programming language that is concurrent, class-based and object-oriented. Compiled Java code can run on all platforms that support Java without the need for recompilation.

Java is available as a module on Apocrita.

Usage

To run the latest installed version of java, simply load the java module:

module load java

then to compile your code use javac

javac HelloWorld.java

and java to run it

java HelloWorld

Oracle JDK

Only Oracle JDK is installed on Apocrita to ensure maximum compatibility.

Memory Issues

JVM Memory Allocation

During the compilation, or execution of your application you might see the errors related to the JVM memory allocation:

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

This is caused by the way java allocates virtual memory and the restrictions placed by the scheduler. The default maximum size of the heap is determined dynamically based on the amount of physical memory available on the system rather than statically, as a result the default values are quite large.

In most cases its highly unlikely that your program will require that much memory, however JVM will try to reserve that regardless. You can override the default maximum heap settings using -Xmx flag:

# Set max to 4G
java -Xmx4G HelloWorld

Alternatively you can set this option in a _JAVA_OPTIONS environment variable which will be automatically picked up by the JVM:

export _JAVA_OPTIONS=-Xmx4G
java HelloWorld

Other memory issues

Heap is not the only memory that JVM uses: thread stacks, java methods, native handlers and JVM internal data structures will use up additional memory. Even if you constrain the maximum heap size you might still experience memory related errors eg.

$ java -Xmx4G HelloWorld
#
# There is insufficient memory for the Java Runtime Environment to continue.
# pthread_getattr_np

Since glibc 2.10 a new dynamic memory allocation behaviour was introduced, which enabled higher scalability across many sockets and cores by assigning threads their own memory pools. As a result this has increased JVM virtual memory consumption. For more detailed explanation please see this blog.

We have set MALLOC_ARENA_MAX=4 in the module file based on the experiments conducted here. Below is an example of the memory usage (in KiB):

$ echo $MALLOC_ARENA_MAX
4
$ java -Xmx5G HelloWorld &
$ ps -o vsz,rss,cmd | grep java
5970880 28156 java -Xmx5G HelloWorld

and without it (default OS setting):

$ echo $MALLOC_ARENA_MAX
$ java -Xmx5G HelloWorld &
$ ps -o vsz,rss,cmd | grep java
8526784 28308 java -Xmx5G HelloWorld

As MALLOC_ARENA_MAX is an environment variable you can either unset it or override its value before the execution of your application, i.e.

export MALLOC_ARENA_MAX=1

Example jobs

Serial job

Here is an example job running on 4 cores.

#!/bin/sh
#$ -cwd
#$ -j y
#$ -pe smp 4
#$ -l h_rt=4:0:0
#$ -l h_vmem=2G

module load java

java -Xmx8G Example

References