java.lang.OutOfMemoryError: increase Java jvm heap space

How to Increase Default Xmx and Xms Values in JVM Using _JAVA_OPTIONS

The Java Virtual Machine (JVM) relies on memory allocation settings to optimize application performance. Two critical parameters, Xmx (maximum heap size) and Xms (initial heap size), control the heap memory available to JVM applications. Adjusting these values is often necessary for memory-intensive applications. This guide explains how to increase the default Xmx and Xms values using the _JAVA_OPTIONS environment variable.



What are Xmx and Xms?

  • Xmx: Defines the maximum heap memory size allocated to the JVM.
  • Xms: Sets the initial heap memory size when the JVM starts.

The JVM uses both parameters to configure memory management. These options enable applications to perform optimally based on their resource needs.


Why Use _JAVA_OPTIONS?

_JAVA_OPTIONS is an environment variable that applies global JVM options to all Java applications on the system. By setting this variable, you can enforce consistent memory settings without modifying individual application configurations.


Steps to Increase Xmx and Xms Values Using _JAVA_OPTIONS

Check Current JVM Memory Settings

Before making changes, verify the current heap memory settings using the following command:

Output Example:

The values are displayed in bytes. For example, 1073741824 bytes is equivalent to 1GB.


Set _JAVA_OPTIONS Environment Variable

To globally set the desired Xmx and Xms values, use the export command with the _JAVA_OPTIONS variable.

Syntax:

Example: To set the initial heap size to 512MB and the maximum heap size to 2GB:

This configuration ensures that all Java applications launched on the system will use these memory settings.



Verify the Updated Settings

Run a simple Java command to confirm the new settings are applied:

Output Example:

The line Picked up _JAVA_OPTIONS confirms that the specified options are applied.


Making _JAVA_OPTIONS Persistent

To ensure the settings persist across sessions, add the export command to your shell configuration file:

Bash: Edit the .bashrc or .bash_profile file:

Zsh: Edit the .zshrc file:

System-Wide Settings: Add the export command to /etc/environment or /etc/profile:

Apply the changes:



Adjusting Settings for Specific Applications

If you only want to change the memory settings for a particular Java application, avoid using _JAVA_OPTIONS and specify the options directly in the command:


Common Issues and Solutions

  1. Error: “Picked up _JAVA_OPTIONS multiple times”:
    • Cause: _JAVA_OPTIONS is set multiple times in environment configurations.
    • Solution: Check all configuration files (~/.bashrc, ~/.bash_profile, /etc/environment) and ensure the variable is defined only once.
  2. Insufficient Heap Memory Error:
    • Cause: The specified Xmx value is lower than required by the application.
    • Solution: Increase the Xmx value based on application requirements and system memory availability.
  3. JVM Fails to Start:
    • Cause: Xms or Xmx values exceed the available system memory.
    • Solution: Verify available memory using free -h and adjust the parameters accordingly.

  1. Oracle JVM Documentation: Command-Line Options
  2. OpenJDK: JVM Options

Leave a Reply

Your email address will not be published. Required fields are marked *