JVM parameter viewing and setting

1 JVM memory management mechanism
1.1 Heap and Non-heap memory
        According to the official statement: "The Java virtual machine has a heap, and the heap is the runtime data area. The memory of all class instances and arrays is from here Allocation. The heap is created when the Java virtual machine starts." "The memory outside the heap in the JVM is called non-heap memory."
        It can be seen that JVM mainly manages two types of memory: heap and non-heap. Simply put, the heap is the memory accessible by the Java code, which is reserved for developers to use; the non-heap is reserved for their own use by the JVM,
        so the method area, the internal processing of the JVM or the memory required for optimization (such as JIT compiled code Cache), each class structure (such as runtime constant pool, field and method data), and the code of methods and construction methods are all in non-heap memory. 

1.1.1 Heap memory allocation
        The heap memory initially allocated by JVM is specified by -Xms, and the default is 1/64 of physical memory; the maximum heap memory allocated by JVM is specified by -Xmx, and the default is 1/4 of physical memory. When the default free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of -Xmx; when the
        free heap memory is greater than 70%, the JVM will reduce the heap until the minimum limit of -Xms. Therefore, the server generally sets -Xms and -Xmx equal to avoid adjusting the heap size after each GC.
        Note: If -Xmx is not specified or the specified value is too small, the application may cause java.lang.OutOfMemory error. This error comes from the JVM and is not Throwable and cannot be caught with try...catch. 
1.1.2 Non-heap memory allocation
        JVM uses -XX:PermSize to set the initial value of non-heap memory, the default is 1/64 of physical memory; XX:MaxPermSize sets the size of maximum non-heap memory, the default is 1/4 of physical memory. (There is another saying: The default value of MaxPermSize is related to the -server -client option
. The default MaxPermSize under the -server option is 64m, and the default MaxPermSize under the -client option is 32m.
        The full name of PermGen space in the above error message is Permanent Generation space, which is Refers to the permanent storage area of ​​the memory. Setting XX:MaxPermSize too small will lead to java.lang.OutOfMemoryError: PermGen space is the memory gain. 
        Why does the memory overflow: 
(1) This part of the memory is used to store Class and Meta information. Class is placed in the PermGen space area when it is loaded, which is different from the Heap area where Instance is stored. 
(2) GC (Garbage Collection) will not clean up PermGen space during the main program runtime, so if your APP will load a lot of CLASS, PermGen space error is likely to occur.
        This kind of error is common when the web server pre-compiles the JSP.

1.2 JVM memory limit (maximum)
        First, JVM memory is limited to the actual maximum physical memory. Assuming that the physical memory is infinitely large, the maximum JVM memory has a great relationship with the operating system. Simply put, although the 32-bit processor has 4GB of controllable memory space, the specific operating system will give a limit,
this limit is generally 2GB-3GB (generally 1.5G-2G under Windows system, and 1.5G-2G under Linux system 2G-3G), and there will be no restrictions on processors above 64bit.

        Why some machines can start Eclipse after I set both -Xmx and -XX:MaxPermSize to 512M, but some machines cannot start?
Through the above introduction to JVM memory management, we have learned that JVM memory includes two types: heap memory and non-heap memory. In addition, the maximum memory of JVM first depends on the actual physical memory and operating system. Therefore, there are several reasons why the program cannot be started by setting VM parameters:
1) The value of -Xms in the parameter is greater than -Xmx, or the value of -XX:PermSize is greater than -XX:MaxPermSize;
2) The value of -Xmx and -XX The sum of :MaxPermSize exceeds the maximum limit of JVM memory, such as the maximum memory limit of the current operating system, or actual physical memory, etc. Speaking of actual physical memory, one thing to note here is that if your memory is 1024MB, it is not possible that 1024MB is used in the actual system because part of it is occupied by hardware.

2 JVM parameter types
2.1 JVM parameter types
1. Standard parameters (-)
All JVM implementations must implement the functions of these parameters and are backward compatible. For example: -verbose:class (output the related information of the jvm loaded class, when the jvm reports that the class cannot be found or the class conflict can be diagnosed); -verbose:gc (output the relevant situation of each GC); -verbose :jni (output related information about native method calls, generally used to diagnose jni call error messages).
2. Non-standard parameters (-X)
default jvm to implement the functions of these parameters, but it does not guarantee that all jvm implementations are satisfied, and backward compatibility is not guaranteed. For example: -Xms512m; -Xmx512m; -Xmn200m; -Xss128k; -Xloggc:file (similar to -verbose:gc function, except that the relevant information of each GC event is recorded to a file, the location of the file is best to be locally, To avoid potential network problems. If it appears on the command line at the same time as the verbose command, the -Xloggc shall prevail).
3. Non-Stable parameters (-XX)
This type of parameter will be different for each jvm implementation, and may be cancelled at any time in the future and needs to be used carefully. For example: -XX:PermSize=64m; -XX:MaxPermSize=512m.


2.2 Standard parameters
1, -help
2, -server -client
3, -version -showversion
4, -cp -classpath


2.3 X parameters
Non-standardized parameters
-Xint: Interpretation and execution
-Xcomp: Compile the
native code at the first use -Xmixed: Mixed mode, JVM decides whether to compile the native code by itself


2.4 XX parameters
Non-standardized parameters
Relatively unstable
Mainly used for JVM tuning and Debug
XX parameter classification:
1. Boolean
format: -XX:[+-]<name> means enabling or disabling the name attribute
 such as: -XX:+UseConcMarkSweepGC
    -XX:UseG1GC

2. Non-Boolean
format: -XX:<name>=<value> indicates that the value of the name attribute is value. For
example: -XX:MaxGCPauseMillis=500
     XX:GCTimeRatio=19

3. -Xmx -Xms
is not the X parameter, but the XX parameter
-Xms is equivalent to -XX:InitialHeapSize
-Xmx is equivalent to -XX:MaxHeapSize

3 JVM parameter view
3.1 View JVM runtime parameters
-XX:+PrintFlagsInitial
-XX:+PrintFlagsFinal
-XX:+UnlockExperimentalVMOptions //Unlock experimental parameters
-XX:+UnlockDiagnosticVMOptions //Unlock diagnostic parameters
-XX:+PrintCommandLineFlags //Print command line parameter

-XX:+UseSerialGC, the default value of the virtual machine running in Client mode, Serial+Serial Old.
-XX:+UseParallelGC, the default value of the virtual machine running in Server mode, Parallel Scavenge+Serial Old (PS Mark Sweep).
-XX:+UseParNewGC, ParNew+Serial Old, are deprecated in JDK1.8, and can be used in JDK1.7.
-XX:+UseParallelOldGC, Parallel Scavenge+Parallel Old.
-XX:+UseConcMarkSweepGC, ParNew+CMS+Serial Old.
-XX:+UseG1GC, G1+G1, only supports JDK1.7 14update and above

java -XX:+PrintCommandLineFlags -version //View the default garbage collector of jvm

3.2 Use of parameters
java -XX:+PrintFlagsInitial -version> PrintFlagsInitial.txt //This command has a lot of values, save it to a file
java -XX:+PrintFlagsFinal -version> PrintFlagsFinal.txt  

3.3 jps View java process
jps //View java process
jps -l //Display the complete class name

3.4 View the running JVM parameters

jinfo -flags pid //View the parameters that have been set
jinfo -flag InitialHeapSize pid //View the initial heap memory
jinfo -flag MaxHeapSize pid //View the maximum heap memory
jinfo -flag PermSize pid //View the initially allocated non-heap memory
jinfo- flag MaxPermSize pid //View the maximum allowable allocation of non-heap memory
jinfo -flag NewSize pid //View the initial memory of the young generation
jinfo -flag MaxNewSize pid //View the maximum memory of the young generation
jinfo -flag NewRatio pid //View the young generation and the old
Jinfo -flag SurvivorRatio pid //View the ratio of the Eden area to the Survivor area in the young generation
jinfo -flag MaxTenuringThreshold pid //View if the object has moved in the Survivor area N times before being garbage collected, it enters the old generation

jinfo -flag UseSerialGC pid //View the serial collector
jinfo -flag UseParallelGC pid //View the parallel collector
jinfo -flag UseParNewGC pid //View the parallel collector
jinfo -flag UseParallelOldGC pid //View the parallel collector
jinfo -flag UseConcMarkSweepGC pid //Check the CMS collector
jinfo -flag UseG1GC pid //Check the G1 collector
jinfo -flag PrintGCDetails pid //Check whether to print the GC log

View JVM memory configuration

jmap -heap pid> 1.txt //There are many parameters, save to file 


4 Common JVM tools
4.1 jps
jps //View the java process
jps -l //Display the complete class name

4.2 jinfo
jinfo -flag InitialHeapSize pid //View the initial heap memory
jinfo -flag MaxHeapSize pid //View the maximum heap memory
jinfo -flag PermSize pid //View the initially allocated non-heap memory
jinfo -flag MaxPermSize pid //View the maximum allowable allocation Non-heap memory
jinfo -flags pid //View the parameters that have been set
jinfo -flag UseConcMarkSweepGC pid //View the garbage collector
jinfo -flag UseG1GC pid //View the garbage collector
jinfo -flag UseParallelGC pid //View the garbage collector

4.3 jmap

jmap -heap pid > 1.txt

5 Tomcat Tuning
5.1 Modifying the memory size of TomcatJVM under Linux
should be added to catalina.sh under tomcat bin, before cygwin=false.
# OS specific support. $var _must_ be set to either true or false.
JAVA_OPTS="-Xms256m -Xmx512m -Xss1024K -XX:PermSize=128m -XX:MaxPermSize=256m" 
cygwin=false


5.2 Modify the memory size of Tomcat JVM under windows
5.2.1 The decompressed version of Tomcat, you must start tomcat through startup.bat to load the configuration

To be added to catalina.bat under tomcat bin

rem Guess CATALINA_HOME if not defined
set CURRENT_DIR=%cd% add after

set JAVA_OPTS=-Xms1024m -Xmx1024m -Xmn=256m -XX:PermSize=128M -XX:MaxPermSize=128m -Djava.awt.headless=true  -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:D:\log\jvm\gc.log

5.2.2 Installed version of Tomcat, no catalina.bat


If there is no catalina.bat under the installed version of Tomcat, if tomcat 6 is registered as a windows service, or the installed version of tomcat is used under windows2003 
, modify it in /bin/tomcat6w.exe.

 

Guess you like

Origin blog.csdn.net/geekooler/article/details/100852538