2016-04-18 20:40:59
Third, JVM memory parameter adjustment and monitoring
3.1 JVM in-memory adjustment
The memory size of the data area of the JVM runtime can be set by parameters, typically two blocks are heap space and persistent generation (method area), the setting method is specified in the form of parameters, Sun's hotspot needs to set these parameters before the JVM starts, cannot dynamically change its size after starting the JVM.
JVM parameter Description:
-xms300m: Heap Space Initial size
-XMX1G: Heap Space Maximum (production environment this value is generally consistent with XMS, which prevents the JVM heap resizing after each garbage collection completes)
-xmn512m: Heap Space Young generation size
The proportions of the young generation and the old age of the-xx:newratio=4:jvm heap were 1:4.
-xss128k: The stack size of each thread is 128K
-xx:survivorratio=6: The Cenozoic surivor region (Cenozoic has 2 surivor zones) and the Eden area is 2:6
-xx:permsize=150m: Persistent Generation Initial size
-XX:MAXPERMSIZE=150M: Maximum value for persistent generation
-xx:maxtenuringthreshold=1: The new generation of objects after several garbage collection (if still survive), into the old age. If this parameter is set to 0, this means that the new generation of objects after garbage collection, do not enter the survivor area, directly into the old age
-xx:+heapdumponoutofmemoryerror: This parameter indicates that when a memory overflow occurs in the JVM, the dump file is automatically generated in the program directory, and the file can be used to analyze what caused the memory overflow
Different Java programs set parameters differently, but the parameter names are the same. Cases:
1. Start a generic Java program you can set the JVM parameters at startup using the following methods
java-xms2g-xmx2g-xmn512m-xss128k-xx:permsize=128m-xx:maxpermsize=128m
-xx:newratio=4-xx:surivorratio=4-xx:maxtenuringthreshold=1
2. The JVM parameters for setting eclipse are typically in the Eclipse.ini file under the Eclipse installation directory;
3. The JVM parameter setting TOMCAT5 is in the Catalina.bat file under Tomcat's Bin directory;
Note: The maximum allocation of JVM heap space is related to operating system and hardware configuration, 32-bit operating system is generally less than 2g, 64-bit operating system can basically be said to be unlimited.
The maximum amount of memory the current operating system can allocate to the JVM can be tested by using "java-xmx1200m" in a command-line window.
3.2 JVM's monitoring tool--jconsole
Knowing how to set up JVM memory, how to monitor the JVM to ensure that the parameters are set correctly, we can monitor the size and usage of the JVM's heap space with the Jconsole monitoring tool that comes with the sun jdk. The steps are as follows:
First step, configure JVM monitoring parameters
This parameter needs to be configured before the JVM starts, with the same configuration method and location as the JVM heap size method.
The specific parameters and descriptions are as follows:
-djava.rmi.server.hostname=127.0.0.1: Specifies the IP of the current host
-dcom.sun.management.jmxremote.port=9880: Specifies the port to use for monitoring
-dcom.sun.management.jmxremote.ssl=false:
-dcom.sun.management.jmxremote.authenticate=false: no Password required
Step two, run the JVM monitoring tool jconsole
Enter "Jconsole" in the Command line window and return (provided the Java environment variable is configured, if it is not configured to go to the JDK's Bin directory to manually execute the "Jconsole.exe" program)
Step three, select the JVM you want to monitor
1. Monitor local JVM
Select the local Java process directly and click Connect
2. Monitor remote JVM
First select remote monitoring, and in the text box to fill in the remote JVM IP address and monitoring port number, if the remote JVM to open password authentication, you also need to enter the user name and password
3.3 JVM's monitoring tool--jprofile
Jprofile is a powerful and best Java profiling tool designed for J2SE and Java EE application monitoring Analytics.
Jprofile supports monitoring of JVMS from a variety of vendors and integrates with popular application servers such as the IDE for JVM monitoring, analyzing memory leaks, and identifying system performance bottlenecks.
Jprofile can monitor both local Java applications and remote Java programs.
Jprofile requires a commercial license to use it and is generally available as a trial version.
Four, the JVM actual combat
4.1 JVM Garbage Collection
(1) Garbage collection concept
What is garbage collection:
This mechanism of automatically detecting and removing data objects that are no longer used in the JVM is called garbage collection (garbage Collection), or GC.
Fundamentals of GC:
The JVM recycles objects that are no longer being used in memory by using the garbage collector and using the appropriate garbage collection algorithm.
Why garbage Collection:
Because different Java objects are not necessarily alive, the entire program crashes after the program runs for a period of time without garbage collection. Garbage collection also organizes fragmented memory fragments, and the most immediate problem with fragmentation is the inability to allocate large chunks of memory space and reduce the efficiency of the program.
Those areas will be GC:
The VM stack, the local method stack, and the program counters are naturally recycled as the method or thread ends, so these areas do not need to consider reclamation issues.
Heap space and persistence generation (method area) are the focus areas of GC recycling, and different area objects are collected differently
(1) The collection of objects for young generations is called minor GC,
(2) The collection of objects for the old age is called full GC. The GC for the active invocation of System.GC () in the program is the full GC.
(2) Garbage collection algorithm
Tag Cleanup algorithm:
Replication algorithm:
Tag Grooming algorithm:
Generational collection algorithm:
(3) Garbage collector
If the collection algorithm is the memory recycling methodology, the garbage collector is the implementation of memory recovery, different garbage collectors have different memory recovery algorithms (reference count, tag-purge algorithm, replication algorithm, marker-collation algorithm, etc.). There is no provision for how the garbage collector should be implemented in the JVM specification, so different vendors, different versions of the JVM, may have a large difference in the garbage collector, and generally provide parameters for the user to assemble the collectors used in each age according to their own application characteristics and requirements.
Understanding Java Virtual machines (bottom)