SoFunction
Updated on 2025-03-06

How JVM adjusts the maximum memory that can be used by Java virtual machines

During the development and deployment of Java applications, rationally configuring the memory parameters of Java virtual machine (JVM) is crucial to improving application performance. This article will introduce in detail how to adjust the maximum memory that Java virtual machine can use through JVM parameters to help developers optimize application performance.

1. Why do you need to adjust the maximum memory of the JVM?

  • Performance optimization: Appropriate memory settings can reduce the frequency of garbage collection and improve the response speed of applications.
  • Resource Management: Ensure that Java applications do not crash due to insufficient memory, while avoiding excessive use of system resources.
  • Adapt to different environments: Different operating environments may require different memory configurations, such as development environments, test environments, and production environments.

2. Introduction to JVM memory model

The memory of a Java virtual machine is mainly divided into the following parts:

  • Heap Memory: used to store object instances and is the main area of ​​garbage collector management.
  • Non-Heap Memory: Mainly including method area, runtime constant pool, native method stack, etc.
  • Program Counter Register: Each thread is private, and is a small piece of memory space used to indicate the location of the bytecode instructions executed by the current thread.
  • Virtual Machine Stack (VM Stack): Each thread is private and is used to store information such as local variables, operand stacks, dynamic links, etc.
  • Native Method Stack: Similar to the virtual machine stack, but serves local methods.

3. Adjust the maximum memory of the JVM

3.1 Heap memory adjustment

The size of heap memory can be adjusted by the following two parameters:

  • -Xms<size>​​: Set the initial heap memory size when JVM starts.
  • -Xmx<size>​​: Set the maximum heap memory size of the JVM.

For example, if you set the initial heap memory to 512MB and the maximum heap memory to 2GB, you can add the following parameters when starting the Java application:

java -Xms512m -Xmx2g -jar 

3.2 Non-heap memory adjustment

The size of non-heap memory can be adjusted by the following parameters:

  • -XX:PermSize=<size>​​: Set the initial size of the Permanent Generation. (Applicable to Java 7 and below)
  • -XX:MaxPermSize=<size>​​: Set the maximum size of the permanent generation. (Applicable to Java 7 and below)
  • ​-XX:MetaspaceSize=<size>​​: Set the initial size of the metaspace. (Applicable to Java 8 and above)
  • -XX:MaxMetaspaceSize=<size>​​: Set the maximum size of the metaspace. (Applicable to Java 8 and above)

For example, if you set the initial size of the metaspace to be 128MB and the maximum size to be 256MB, you can add the following parameters:

java -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -jar 

3.3 Other memory-related parameters

  • ​-XX:NewRatio=<proportion>​​: Set the ratio between the new generation and the old generation. For example, setting to 2 means that the new generation accounts for 1/3 of the entire heap memory.
  • ​-XX:SurvivorRatio=<proportion>​​: Set the ratio of Eden area to Survivor area. For example, setting to 8 means that the Eden area accounts for 8/10 of the Celestial Generation.

4. Monitoring and tuning

After adjusting JVM memory parameters, it is recommended to use monitoring tools to observe the performance changes of the application. Common monitoring tools are:

  • VisualVM: A graphical tool that can monitor the memory usage, thread status, etc. of the JVM.
  • JConsole: JDK comes with monitoring tools, providing detailed JVM monitoring data.
  • Prometheus + Grafana: Can be integrated into the continuous monitoring system to provide a richer visual interface.

Rationally configuring the memory parameters of the JVM is an important step in optimizing Java application performance. Through the methods introduced in this article, developers can adjust the memory settings of the JVM according to actual needs, thereby improving the stability and performance of the application. Hope this article helps you!

Commonly used JVM memory related parameters

  • -Xms<Memory Size>​​: Set the initial heap memory size when JVM starts.
  • -Xmx<Memory Size>​​: Set the maximum heap memory size that the JVM can use.
  • -Xss<thread stack size>​​: Set the stack size of each thread.

Sample Scenario

There is a Java web application running on a Tomcat server that needs to handle a large number of concurrent requests, and each request may involve a large amount of data processing. To ensure that the application has enough memory to handle these requests efficiently, you need to adjust the maximum heap memory of the JVM.

1. Modify the startup script of Tomcat

If your application is deployed on Tomcat, you can set the maximum memory of the JVM by modifying the Tomcat startup script. Usually, this file is located in​bin​In the directory, named​​(Linux/Unix) or​​​(Windows)。

For Linux/Unix systems (​​​):

#!/bin/sh
# Set the initial and maximum heap memory of JVM to 2GBexport CATALINA_OPTS="$CATALINA_OPTS -Xms2g -Xmx2g"
# Set the stack size of each thread to 512KBexport CATALINA_OPTS="$CATALINA_OPTS -Xss512k"

For Windows systems (​​​):

@echo off
rem set upJVMInitial and maximum heap memory2GB
set CATALINA_OPTS=%CATALINA_OPTS% -Xms2g -Xmx2g
rem set up每个线程的栈大小为512KB
set CATALINA_OPTS=%CATALINA_OPTS% -Xss512k

2. Start Java application directly on the command line

If your application is not deployed in a container, but is started directly through the command line, you can directly specify the JVM parameters in the startup command.

java -Xms2g -Xmx2g -Xss512k -jar 

Things to note

  • Memory allocation: Ensure that the allocated memory does not exceed the physical memory of the system, otherwise it may lead to frequent disk swaps and seriously affect performance.
  • Monitoring and tuning: In a production environment, it is recommended to use monitoring tools (such as JVisualVM, Prometheus, etc.) to monitor the memory usage of the JVM and adjust it according to the actual situation.
  • Garbage recycling: While adjusting memory, you can also consider adjusting the parameters of the garbage collector to improve performance.

Through the above method, you can effectively adjust the memory settings of the JVM to adapt to different application scenarios and needs. In Java applications, adjusting the memory settings of a Java virtual machine (JVM) is a common optimization step, especially for applications that need to process large amounts of data or run complex computing. JVM memory is mainly divided into heap memory and non-heap memory. Among them, heap memory is mainly used to store object instances, while heap memory is not used to store method areas, class information, constant pools, etc.

Heap memory adjustment

The size of heap memory can be specified by command line parameters when starting Java applications. The two commonly used parameters are:

  • ​-Xms<initial heap size>​​: Set the initial heap memory size allocated at the start of the JVM.
  • ​-Xmx<maximum heap size>​​: Set the maximum heap memory size that the JVM can use.

For example, if you want your Java application to allocate 128MB of initial heap memory when starting, and can use up to 512MB of heap memory, you can set it like this in the startup command:

java -Xms128m -Xmx512m -jar 

Non-heap memory adjustment

The size of non-heap memory can also be adjusted through command line parameters:

  • ​-XX:PermSize=<initial size>​​: Set the initial size of the permanent generation (PermGen, suitable for Java 7 and earlier).
  • ​-XX:MaxPermSize=<maximum size>​​: Set the maximum size of the permanent generation.
  • ​-XX:MetaspaceSize=<initial size>​​: Set the initial size of the metaspace (Metaspace, suitable for Java 8 and later).
  • ​-XX:MaxMetaspaceSize=<maximum size>​​: Set the maximum size of the metaspace.

For example, if you want to set the initial size of the metaspace to be 64MB and the maximum size to be 256MB, you can do this:

java -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -jar 

Other related parameters

In addition to the basic memory settings mentioned above, there are some other parameters that can help you better control the behavior of the JVM, such as:

  • ​-XX:+UseConcMarkSweepGC​​: Enable concurrent marker removal garbage collector.
  • ​-XX:+UseG1GC​​: Enable G1 garbage collector.
  • ​-XX:NewRatio=<ratio>​​: Set the ratio between young generation and old generation. For example,​-XX:NewRatio=3​​ indicates that the ratio of young generation to old generation is 1:3.
  • ​-XX:SurvivorRatio=<ratio>​​: Set the ratio of Eden area and Survivor area in the young generation. For example,​-XX:SurvivorRatio=8​​ Indicates that the ratio of Eden area to a Survivor area is 8:1.

Example

There is a Java application that requires a higher memory configuration and wants to use the G1 garbage collector, and has specific requirements for the ratio of young generations and older generations. You can configure the JVM like this:

java -Xms512m -Xmx2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -XX:NewRatio=2 -jar 

In this example:

  • The initial heap memory is 512MB and the maximum heap memory is 2GB.
  • The initial size of the metaspace is 128MB and the maximum is 512MB.
  • Use the G1 garbage collector.
  • The ratio of young generation to old generation is 1:2.

By rationally adjusting these parameters, the performance of Java applications can be effectively optimized, especially in handling large data volumes or high concurrency scenarios.

This is the article about how JVM adjusts the maximum memory that can be used by Java virtual machines. For more related content on JVM adjusting the maximum memory of virtual machines, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!