Estimated reading time: 3 minutes
If you’ve ever dealt with Java-based applications crashing, you might have seen the dreaded java.lang.OutOfMemoryError: Java heap space
. This error usually means your application has exhausted the allocated memory, and it’s incredibly frustrating when you know your server has enough RAM, like an 8GB setup. Recently, I had this issue on my Ubuntu 22.04 server when executing a jar file for some internal routine tasks and had to look into Java heap size adjustments.
This made me dig into the different options for adjusting Java’s memory settings, mainly using the export _JAVA_OPTIONS
environment variable and command-line arguments. Here’s how you can tweak these to prevent those annoying OutOfMemoryError
crashes.
Table of contents
Using export _JAVA_OPTIONS to increase Java heap space
One of the easiest ways to manage heap size for your Java applications is by using the export _JAVA_OPTIONS
environment variable. This setting applies globally, affecting any Java process that starts after you set it. For example, to increase the heap size, you can use:
export _JAVA_OPTIONS="-Xms1024m -Xmx4096m"
-Xms1024m
: Sets the initial heap size to 1 GB.-Xmx4096m
: Sets the maximum heap size to 4 GB.
The great thing about _JAVA_OPTIONS
is that it applies to all Java applications running in your environment. You can add this to your .bashrc
or .profile
to make it persistent across reboots:
echo 'export _JAVA_OPTIONS="-Xms1024m -Xmx4096m"' >> ~/.bashrc
source ~/.bashrc
This way, you ensure that every Java application on the system has sufficient heap space allocated.
Passing Command Line Arguments to Java
Sometimes, you may want more control over individual Java processes. In such cases, you can pass the memory configuration directly as command-line arguments when launching a Java application. For instance:
java -Xms1024m -Xmx4096m -jar myapp.jar
This method is especially useful when you’re running standalone JAR files or want specific memory settings for different Java apps. This is the approach I used when running my routine internal tasks on the Ubuntu 22.04 server. It allowed me to fine-tune the heap size without affecting other Java processes.
Configuring Java Heap Size with CATALINA_OPTS
for Tomcat
You’ll need to adjust the heap size using the CATALINA_OPTS
environment variable if you’re using a Tomcat server, as I am for some of our applications. This is the Tomcat-specific way of configuring memory allocation.
1. Open the setenv.sh
file in your Tomcat installation directory:
sudo vi /opt/tomcat/bin/setenv.sh
2. Add the following line:
export CATALINA_OPTS="-Xms1024m -Xmx4096m"
This configuration sets the heap size for Tomcat applications only, allowing them to handle larger memory loads without affecting other Java processes running on the same machine. After saving the file, restart Tomcat to apply the changes:
sudo systemctl restart tomcat
Why These Options Matter
The combination of export _JAVA_OPTIONS
, command-line memory flags, and CATALINA_OPTS
gives you control over how Java uses memory across different environments. Setting global options through _JAVA_OPTIONS
ensures system-wide stability, while command-line arguments let you fine-tune memory for specific applications. CATALINA_OPTS
, on the other hand, ensures Tomcat has its dedicated memory configuration.
Monitoring Memory Usage
Once you’ve increased your heap size, monitoring memory usage is a good idea to ensure you aren’t allocating too much memory, which could lead to long garbage collection pauses. Tools like VisualVM or JConsole allow you to analyze your application’s memory usage in real time.
Conclusion
By correctly configuring heap size using export _JAVA_OPTIONS
, command-line arguments, and CATALINA_OPTS
, you can prevent the frustrating OutOfMemoryError crashes in your Java applications. Adjusting these settings, particularly on systems with abundant RAM, ensures that your applications run smoothly without frequent memory-related errors.