Detailed explanation of JVM memory settings

This article mainly introduces the JVM memory settings: 2 methods.
When installing Java development software, the default installation contains two folders, one JDK (Java Development Kit), one JRE (Java Runtime Environment, including JVM), and the JDK contains another JRE. If you just run Java programs, the JRE is enough; the JDK is only used by developers. Talking about 2 coups for JVM memory settings:

 

1. View JVM memory setting information
Runtime.getRuntime().maxMemory(); //Maximum available memory, corresponding to -Xmx
Runtime.getRuntime().freeMemory(); //Current JVM free memory
Runtime.getRuntime().totalMemory( ); //The total amount of memory occupied by the current JVM, its value is equivalent to the sum of the memory used by the current JVM and freeMemory()
About maxMemory(), freeMemory() and totalMemory():
maxMemory() is the maximum available memory of the JVM, It can be set by -Xmx, the default value is 1/4 of the physical memory, and the setting value cannot be higher than the physical memory of the computer;
totalMemory() is the total memory occupied by the current JVM, and its value is equivalent to the memory used by the current JVM and freeMemory() The sum of the JVM will increase as the memory used by the JVM increases;
freeMemory() is the current JVM free memory, because the JVM only occupies physical memory when it needs memory, so the value of freeMemory() is generally very small, while The actual available memory of the JVM is not equal to freeMemory(), but should wait for maxMemory()-totalMemory()+freeMemory(). and its setting JVM memory allocation.

 

Second, set the JVM memory settings

1. There are four parameters for setting the JVM memory:
-Xmx Java Heap maximum value, the default value is 1/4 of the physical memory, the best setting value should depend on the physical memory size and other memory overhead in the computer;
-Xms Java Heap The initial value, the server-side JVM is best to set -Xms and -Xmx to the same value, the development and testing machine JVM can keep the default value;
-Xmn Java Heap Young area size, if you are not familiar with it, it is best to keep the default value;
-Xss The value of each thread Stack size, if you are not familiar with it, it is best to keep the default value;

 

2. How to allocate JVM memory settings:
(1) When starting and using the JVM at the command prompt (only valid for the currently running class Test):
java -Xmx128m -Xms64m -Xmn32m -Xss16m Test

(2) When the JVM is started and used in an integrated development environment (such as eclipse):
a. Open eclipse.ini in the eclipse root directory, the default content is (the JVM memory allocation for running the current development tool is set here):
- vmargs  
-Xms40m 
-Xmx256m 
-vmargs indicates that the following parameters are set for the virtual machine, the parameter values ​​can be modified, and -Xmn, -Xss can also be added. In addition, non-heap memory can also be set in eclipse.ini, such as: -XX:PermSize =56m, -XX:MaxPermSize=128m.
The parameter values ​​set here can be displayed in the status bar of the development tool through the following configuration:
Create a file options in the eclipse root directory, the file content is: org.eclipse.ui/perf/showHeapStatus=true
Modify eclipse in the eclipse root directory. ini file, add the following at the beginning:
-debug options 
-vm javaw.exe 
Restart eclipse, you can see more JVM information in the status bar below.
b. Open eclipse-window-preference-java-installed JRE (valid for all java programs running in the current development environment) to
edit the currently used JRE, and enter in the default VM parameters: -Xmx128m -Xms64m -Xmn32m -Xss16m
c. Open eclipse - run - run - Java application (only valid for the set java class),
select the class that needs to set memory allocation - argument, enter in the VM argument: -Xmx128m -Xms64m -Xmn32m - Xss16m
Note: If b and c are set at the same time in the same development environment, the b setting is valid, and the c setting is invalid. For example,
the setting of the development environment is: -Xmx256m, and the setting of the class Test is: -Xmx128m -Xms64m, then run The settings that take effect during Test are:
-Xmx256m -Xms64m

 

(3) When the JVM is started and used in the server environment (such as Tomcat) (valid for the Java program in the current server environment):
a. Set the environment variable:
variable name: CATALINA_OPTS
variable value: -Xmx128m -Xms64m -Xmn32m -Xss16m
b. Open the bin folder in the root directory of Tomcat, edit catalina.bat, and replace %CATALINA_OPTS% (four places in total) with: -Xmx128m -Xms64m -Xmn32m -Xss16m

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040349&siteId=291194637