Java garbage collection (garbage Collection,gc)
At some point, an object that has more than one reference (rreference) points to it, then the object is Alive (live),
Otherwise death (Dead), considered as rubbish, can be recycled and reused by the garbage Collector.
the Garbage collection operation consumes cpu, thread, time and other resources , so it is easy to understand that the garbage collection operation is not happening in real time (the object will not be released immediately after death).
A garbage collection action is triggered when the memory is consumed or when an indicator is reached (Threshold, which uses memory as the Billy of Total memory).
Three things the GC needs to do:
what memory needs to be recycled?
--how to tell if the object is dead? Using root search algorithm
The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search downward, the path that the search passes is called the reference chain (Reference Chain).
When an object is connected to a GC roots without any reference chain, it proves that the object is not Available.
in the Java language, the objects that can be used as GC roots include the Following:
the referenced object in the virtual machine stack (the local variable table in the stack frame).
The object referenced by the class static property in the method Area.
The object referenced by a constant in the method Area.
The object referenced by the JNI (that is, the generally said native METHOD) in the local method stack.
When do I recycle?
A garbage collection action is triggered when the memory is consumed or when a threshold is reached (Threshold, which uses memory as the Billy of Total memory). Thresholds can be configured with JVM Parameters.
How to recycle?
The basic algorithm of GC mechanism is the generation of collection,
When the new generation of Eden is full, the minor gc, also known as the young gc, is Executed. The replication algorithm is Used.
The major gc, also called the full gc, will be executed when the old age is low on memory. A tag-clear algorithm or a tag-collation algorithm is Used.
In the GC mechanism, it is the garbage collector that plays an important role, and the garbage collector is the specific implementation of GC. Such as
New Generation Collectors:
- Serial collector: Use the stop copy algorithm to use one thread for gc, serial, and other worker threads to Pause. USE-XX:+USESERIALGC to run memory reclamation using serial+serial old mode (this is also the default value that the virtual machine runs in client Mode)
- Parnew collector: Use the stop copy algorithm, serial The multi-threaded version of the collector, use multiple threads for gc, parallel, other worker threads to pause, and focus on shortening the garbage collection Time. Use THE-XX:+USEPARNEWGC switch to control the collection of memory using the parnew+serial old collector combination, and use-xx:parallelgcthreads to set the number of threads that perform memory Reclamation.
- Parallel Scavenge collector: Use the Stop copy algorithm to focus on CPU throughput, that is, the time/total time to run user code, such as: the JVM runs for 100 minutes, where the user code runs for 99 minutes, garbage collection 1 minutes, the throughput is 99%, This collector is the most efficient use of the cpu, suitable for running background operations (focus on reducing garbage collection time collector, such as cms, waiting time is very small, so suitable for user interaction, improve the user experience). Use THE-XX:+USEPARALLELGC switch to control the garbage collection using the parallel scavenge+serial old collector (which is also the default value in Server mode); use-xx: Gctimeratio to set the proportion of user execution time to total time, default 99, which is 1% of the time used for garbage collection. Use-xx:maxgcpausemillis to set the maximum pause time for GC (this parameter is only valid for parallel scavenge), and the switch Parameter-xx:+useadaptivesizepolicy can be used for dynamic control, such as automatic adjustment eden/ Survivor ratio, age of Old age, Cenozoic size, etc., This parameter is not in the PARNEW.
Old age Collectors:
- Serial old collector: single-threaded collector, serial, using Tag collation (collation method is sweep (clean) and compact (compression), cleanup is to kill discarded objects, only the surviving objects, compression is moving objects, will fill the space to ensure that the memory is divided into 2 pieces, a piece of all objects, An Idle) algorithm that uses a single thread for gc, other worker threads to pause (note that the tagging algorithm cleans up in the old age, and other threads also need to be paused), before JDK1.5, the Serial old collector is used in conjunction with Parallelscavenge.
- Parallel old collector: older collector, multi-threaded, parallel, multi-threaded mechanism and Parallel scavenge good, use tag collation (unlike serial, Here is the summary (summary) and compact (compression), The summary means that the surviving objects are copied to the pre-prepared area, rather than the algorithm that cleans up the discarded objects like sweep (cleanup), and the other threads still need to be paused when parallel old Executes. Parallel old is useful in multicore computing. Parallel old appeared (JDK 1.6), with the Parallel scavenge with a good effect, fully embodies the Parallel scavenge collector throughput First Effect. Use THE-XX:+USEPARALLELOLDGC switch to control the collection using the Parallel scavenge +parallel old combo Collector.
- CMS (Concurrent Mark Sweep) collector: The old age collector, dedicated to obtaining the shortest payback time (I.E. shortening the time of garbage collection), using the tag cleanup algorithm, multi-threading, The advantage is concurrent collection (the user thread can work concurrently with the GC thread), the pause is Small. USE-XX:+USECONCMARKSWEEPGC for memory reclamation of Parnew+cms+serial old, take precedence over Parnew+cms (see later), and when the user thread is out of memory, use the fallback scenario Serial old Collection.
G1 Collector: officially released in JDK1.7, with the current situation of the new generation, the concept of old age is very different, the current use less, do not introduce.
In-depth understanding of java:1.3. garbage collection