One, the JVM memory model summarizes
There is also a register where the thread runs on top of it
1. Program counter
Record the execution location of the thread, the thread private memory, the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions
2. Thread stacks (VM stack)
The default size of the stack is 1M
-xss2m This setting to 2M
Exception: Fatal:stack size too small
Exceptions are usually caused by too many threads
3. Local method stack (native stack)
The stack that is allocated for some native methods
Exception: Java.lang.OutOfMemoryError:unable to create new native thread
Generally also caused by too many threads, increase the stack space, the same method
4. Heap, program available heap
From Jconsole
Each thread's stack is private to the thread, and the heap is shared by all threads.
The heap mentioned here, mainly refers to the program can control, including
The New generational Heap, default 4M, this area is typically 1/15 of the size of the JVM's memory
This generation is divided into Eden Space area, Survivo space area can be regarded as emptySurvivo area, Survivor District,
When new is an object, first in the Eden Space area, when the Eden space is full, the Survivor space area is garbage collected (copy algorithm here), and the object is recycled several times in the Survivo space area tenured Generation
The copy algorithm, each time the algorithm starts to stop all the current threads, and then the survivor area of all active objects copied to the emptysurvivo area, and then the survivor area to clear the space into emptySurvivo , the former emptySurvivo became the Survivor District. Interchangeable
Tenured Generation,
This is where older generations are stored, where garbage collection uses the Mark and Sweep algorithm
The GC tag algorithm/cleanup algorithm (the mark and Sweep algorithms) is recycled, tagged from a reference, and then reclaimed by the degree of reference or non-reference to the object, and then merges the purged memory.
About GC, because GC is primarily a collection of heaps, and of course there are constant pools and permanent generations, so here's a summary of the GC
Introduction to GC Policies
There are three common types of GC in HotSpot VMS:
1.serial Collector, single-threaded collector , will need to suspend the current thread for a long time to recycle,
Configuration
Default mode under Client
Force plus-XX:+USESERIALGC
2.parallel Collector ( throughput collector ), parallel collector, or multi-threaded collection ,
Young generation: Pauses the application, which collects multiple garbage collection threads in parallel for replication.
Older generation: Pauses application, multiple garbage collection threads in parallel replication collection.
Default mode under Server, specific configuration
Set the number of threads that are collected in parallel, such as 20 threads,-xx:parallelgcthreads=20
Configure young to collect-XX:+USEPARALLELGC in parallel.
Configure the old generation garbage collection method for parallel collection (JDK6.0 start support)-XX:+USEPARALLELOLDGC
Set the pause time to set the maximum time for each young generation of garbage collection if this time is not met, the JVM will automatically adjust the younger generation size to meet this value, such as-xx:maxgcpausemillis= 100,
Set throughput, throughput is the ratio of garbage collection time to non-garbage collected time-xx:gctimeratio to adjust GC time
3.concurrent Collector (Concurrent low pause collector), concurrent collector ,
Young generation: The same is the pause application, which collects multiple garbage collection threads in parallel for replication.
The old generation: and parallel differences in this, only in the initial mark (initial mark) and two times mark (remark) need stop-the-world. But the collection time is very long, so can not wait for the younger generation to start to clean up again.
Using the start concurrency collector-XX:+USECONCMARKSWEEPGC
xx:cmsinitiatingoccupancyfraction= Specifies how many remaining heaps will begin to perform concurrent collection
- According to the official documentation, both of them need to be in a multi-CPU situation to work. In the case of a CPU, it would be inferior to the default serial collector, because thread management consumes CPU resources. And in the case of two CPUs, it also raises little. It only increases in the case of more CPUs. Of course concurrent low Pause collector has a pattern that can be used on less CPU machines, providing as few pauses as possible, see CMS GC Incremental mode.
- When you want to use throughput collector, add-XX:+USEPARALLELGC to Java opt and start throughput collector collection. You can also add-xx:parallelgcthreads=<desired number> to change the number of threads. There are also two parameters-xx:maxgcpausemillis=<nnn> and-xx:gctimeratio=<nnn>,maxgcpausemillis=<nnn> to control the maximum pause time, while the-XX : Gctimeratio can increase the ratio of the GC to the CPU, reducing the heap by the largest number of words.
-This paragraph is quoted from types of HotSpot VM GC
GC trigger conditions (based on serial collector, different GC strategies are slightly different, basically similar)
For new generation, when the object in the Eden area is empty greater than Survivor0 (assuming the from), a minor GC occurs, which is a copy algorithm garbage collection for the entire Survivor0 area, Survivor1 area, And some objects go to old Generation
When new generation is full (that is, when the Eden Zone +survivor0 is greater than the old generation's free space), a major GC will occur, that is, garbage collection of new generation and old generation two generations
Of course there is a program called System.GC (), but this method is not necessarily called, but it is recommended
As a result, if the JVM's setup memory is too large, the GC recovery frequency will be smaller, but the longer the recovery time (especially for the new generation replication algorithm replication is required to stop all current threads), it is not said that the JVM's memory settings, the greater the better, should be optimized according to the actual situation.
Exception Java.lang.OutOfMemoryError:Java Heap Space
It is generally due to garbage collection, old generation space is not enough
Related parameter settings for heap memory
Default value (based on-server)
-server The maximum heap memory is 1/4 of physical memory, but less than 1G. JDK 1.5 used to be 64M
(Official: Smaller of 1/4th of the physical memory or 1GB. Before J2SE 5.0, the default maximum heap size was 64MB.)
-client Small One-fold
Parameter settings
-xms128m
Represents the JVM heap (heap memory) minimum size 128MB, initial allocation
-xmx512m
Represents the maximum allowable size of the JVM heap (heap memory) 256MB, on Demand
The ratio of new generation to old generation, which defaults to 1:2, is 2
The-xx:newratio= parameter can be set (you can also set the initial and maximum values for the new field-xx:newsize and-xx:maxnewsize)
The ratio of Eden to Survivor, default to 32
The-xx:survivorration= parameter can be set
When the object defaults to 1 Times New Generation to the old Generation (this different article is different, I'm sure)
-xx:maxtenuringthreshold= parameter can be set (default 0)
Use-xx:+printtenuringdistributio to view values
5. Method area, Permanent generation (Perm Space)
can also be seen as part of the heap memory, as a permanent generation (Perm Space), but the GC will also be recycled, but rarely recycled, it is used to store the virtual machine loaded by the class information, constants, static variables, the real-time compiler compiled code and other data, such as an int x, in different environments are different, This information exists in the method area
Exception: Java.lang.OutOfMemoryError:PermGen space
Cause, generally is too much class information, such as the application of spring a lot of reflective information, can be moderately set large point
Method area or immortal generation related settings
-XX:PERMSIZE=64MB minimum size, initial allocation
-XX:MAXPERMSIZE=256MB maximum allowable allocation size, on demand
Xx:+cmsclassunloadingenabled-xx:+cmspermgensweepingenabled Set Garbage not recycled
Default size
-server option, the default maxpermsize is 64m
-client option, the default maxpermsize is 32m
6. Constant Pool
class file In addition to the class version, fields, methods, interfaces and other information such as descriptions, there is also a constant table (Constant_pool table), for the compilation of the known constants, which will be loaded in the class load into the method area (permanent generation) storage. However, the Java language does not require constants to enter the method area constant pool only if the content of the const table of the class is pre-compiled, and the new content can be put into Chang (the most typical String.intern () method) during the run time.
The role of GC is mainly used to unload classes and recycle constant pool, of course, there are some method areas, even if the permanent generation (Perm Space) will be a certain recycling
7.native Memory Area "Code Cache" (non-heap)
This is not drawn, but the tool can be seen to hold the memory for compiling and saving the local code (native)
Second, the Tomcat monitoring tuning
1, Monitor Tomcat memory
JConsole, JDK comes with
Enter jconsole directly at the command line, open jconsole, such as the heap that graph, you can monitor the
Remote monitoring, you need to configure com.sun.management.jmxremote when you start the program
Like Tomcat,
Open%tomcat_home%/bin/catalina.bat (Linux is catalina.sh), add parameters after java_opts=%java_opts%
-dcom.sun.management.jmxremote.port=1090-dcom.sun.management.jmxremote.ssl=false- Dcom.sun.management.jmxremote.authenticate=false
Then open Jconsole.
Then you can connect
If you open the heap diagram, you can find all the parts mentioned above.
Eden Space: Memory is initially allocated to most objects from this thread pool.
Survivor Space (heap): Used to save objects from Eden Space copy, the copy algorithm, in two parts Survivor
Tenured Generation (heap): Used to hold objects that have been in the Survivor space memory pool for some time.
Perm Generation (Non-heap): A method area that holds the virtual machine's own static (refective) data, such as class and method objects. Java Virtual machines share these class data. This area is divided into read-only and write-only
Code cache (NON-HEAP): The HotSpot Java Virtual machine includes a memory for compiling and saving the local code (native code), called the "Codes cache"
2. Print Tomcat logs
Continue with parameter-xloggc:%tomcat_home%\logs\tomcat_gc.log in front to output log log file to Tomcat_gc.log
Open the log as follows
0.755 : [GC 11747K->1664K(62848K), 0.0039655 secs] 0.759 : [Full GC 1664K->1604K(62848K), 0.0241215 secs] |
If not satisfied, can be tuned, parameters as shown in the above parameters, directly set in the Java_otps can be
3, use Jsta to view GC status
Detailed attached
JConsole Remote monitoring Tomcat service
Tomcat parameters in a detailed
Jstat Use
Other tools
Jprofiler : Commercial software, you pay for it. Powerful. Detailed description Reference
VisualVM : JDK comes with, powerful, similar to Jprofiler, official website address download http://visualvm.java.net/download.html (of course JDK7 are now self-brought, in jdk1.7.0_ 10\bin)
Jconsole and VISUALVM in the JDK have their own O (∩_∩) o haha feel jprofiler these charges are quite embarrassing
OK, garbage collection and tuning principle after finishing, probably clear, tuning has a direction, specific parameters can be checked documents, etc.
"Go" JVM memory model and memory tuning