JVM memory management and JVM garbage collection
JVM Memory Composition Structure
The JVM memory structure consists of heap, stack, local method stack, method area and so on, and the structure diagram is as follows:
1) Heap
All the memory of objects created by new is allocated in the heap, and its size can be controlled by-XMX and-XMS. The heap is divided into the Cenozoic and the old generation, and the Cenozoic is further divided into the Eden and survivor areas, and the final survivor is composed of Fromspace and tospace, and the structure diagram is as follows:
Cenozoic. New objects are used to allocate memory in the Cenozoic, when Eden space is insufficient, the surviving objects will be transferred to the survivor, the Cenozoic size can be controlled by-xmn, you can also use-xx:survivorratio to control the proportions of Eden and survivor. The old generation is used to store objects that are still alive in the Cenozoic after multiple garbage collection (also known as minor GC)
2) stack
Each thread executes each method by applying a stack frame in the stack, each of which includes a local variable area and an operand stack to hold temporary variables, parameters, and intermediate results during the method call.
3) Local Method stack
Used to support the execution of the native method, which stores the state of each native method call
4) Method Area
Contains the class information to load, static variables, constants of the final type, properties, and method information. The JVM uses durable generation (permanetgeneration) to store the method area, and the minimum and maximum values can be specified by-xx:permsize and-xx:maxpermsize. After introducing the JVM memory composition structure, let's look at the JVM garbage collection mechanism.
JVM garbage collection mechanism
The JVM uses different garbage collection mechanisms for the Cenozoic and the old generation respectively
New generation of GC:
The new generation usually has a shorter survival time, so the so-called copying algorithm is used to retrieve the surviving object based on the copying algorithm, and copy it into a completely unused space, corresponding to the Cenozoic, which is copy between Eden and Fromspace or Tospace. The Cenozoic uses a free pointer to control the GC trigger, the pointer keeps the last allocated object in the Cenozoic interval, and when a new object is allocated memory, it is used to check if the space is sufficient and not enough to trigger the GC. When objects are continuously allocated, the objects gradually go from Eden to Survivor, and finally to the old generation,
Using JAVAVISUALVM to view, can obviously observe the new generation full after the object will be transferred to the old generation, and then empty continue to load, when the old generation is full, will be reported OutOfMemory exception, as shown:
JVM provides serial GC (SERIALGC), parallel reclaim GC (Parallelscavenge), and parallel GC (PARNEW) on execution mechanism
1) Serial GC
The entire scanning and copying process is a single-threaded way, suitable for single CPU, the new generation of small space and the demand for pause time is not very high application, is the client level of the default GC mode, can be-XX:+USESERIALGC to enforce the specified
2) Parallel Recovery GC
In the entire scanning and replication process in a multi-threaded way, for multi-CPU, the time required for a short pause on the application, the server level is the default use of GC mode, can be-XX:+USEPARALLELGC to enforce the designation, with-XX: Parallelgcthreads=4 to specify the number of threads
3) Parallel GC
Use with concurrent GC for legacy generations
GC for old generation:
The old generation and the new generation, the object survival time is longer, more stable, so the mark (Mark) algorithm for recycling, so-called Mark is to scan out the surviving objects, and then to reclaim unmarked objects, after recycling the empty space is either merged, or marked out for the next allocation, The bottom line is to reduce the loss of efficiency caused by memory fragmentation. The JVM provides a serial GC (SERIALMSC), parallel GC (PARALLELMSC), and concurrent GC (CMS) on the execution mechanism, and the details of the algorithm need to be further studied.
The various GC mechanisms above need to be combined, as specified in the following table:
This article transferred from: http://developer.51cto.com/art/201103/248642.htm
Explanations for the minor GC and full GC:
- New Generation GC (Minor GC): Refers to garbage collection actions occurring in the new generation, as Java objects are mostly
Preparation of the characteristics of the Minor, so the GC is very frequent, the general recovery speed is relatively fast.
- Old age GC (Major gc/full GC): Referring to the GC that occurred in the old age, the Major GC appeared, often
is accompanied by at least one Minor GC (but not absolute, in the collection strategy of the Parallelscavenge collector
There is a policy selection process that directly Major GC). MAJORGC speed is generally 10 slower than Minor GC
Times above.
The virtual machine defines an object age counter for each object. If the object is still alive after Eden was born and after the first Minor GC, and can be accommodated by Survivor, it is moved to Survivor space and the object age is set to 1. Object in the Survivor area each time Minor GC, age increases by 1 years, when its age to a certain extent (by default, 15 years old), will be promoted to the old age. The age threshold of an object's promotion to the old age can be set by parameter-xx:maxtenuringthreshold.
Detailed explanation of how Java GC works +minor GC, FULLGC