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:
java -XX:+PrintFlagsFinal -version | grep HeapSize
Output Example:
size 67108864 InitialHeapSize
size 1073741824 MaxHeapSize
size 16777216 MinHeapSize
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:
export _JAVA_OPTIONS="-Xms<size> -Xmx<size>"
Example: To set the initial heap size to 512MB and the maximum heap size to 2GB:
export _JAVA_OPTIONS="-Xms512m -Xmx2g"
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:
Picked up _JAVA_OPTIONS: -Xms512m -Xmx2g
java version "17.0.3" 2022-04-19 LTS
Java(TM) SE Runtime Environment (build 17.0.3+7)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.3+7, mixed mode)
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:
echo 'export _JAVA_OPTIONS="-Xms512m -Xmx2g"' >> ~/.bashrc
Zsh: Edit the .zshrc
file:
echo 'export _JAVA_OPTIONS="-Xms512m -Xmx2g"' >> ~/.zshrc
System-Wide Settings: Add the export command to /etc/environment
or /etc/profile
:
sudo echo 'export _JAVA_OPTIONS="-Xms512m -Xmx2g"' >> /etc/environment
Apply the changes:
source ~/.bashrc # or ~/.zshrc
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:
java -Xms512m -Xmx2g -jar my-application.jar
Common Issues and Solutions
- 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.
- Cause:
- 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.
- 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.