Welcome to the official account ： Wuxiaozhu
What is rubbish
The memory is not in use , It's garbage .
Java Memory in is dynamically allocated and automatically recycled . Learn about garbage collection , Tuning strategy , Can help us deal with and deal with all kinds of work in the face of memory leaks .
Java Virtual machine runtime data is divided into program counters 、 Virtual machine stack 、 Native Method Stack 、 Pile up 、 Method area .
Where the program counter 、 Virtual machine stack 、 Local method stack here 3 Areas are thread private , Will be recycled automatically as the thread dies , So there's no need to manage .
So garbage collection only needs to focus on heap and method area .
Java Memory allocation
- If the local thread allocation buffer is started , Will be allocated by thread priority in TLAB On .
- Priority is given to Eden The distribution of
- Big target into the old generation
- Long term survivors enter the old age
On the stack
Based on escape analysis technology , If an object is always in a method . Make sure the object doesn't escape from the method , Let this object allocate memory on the stack , In this way, the object will be destroyed automatically with the end of the method , It reduces the pressure of garbage collection .
How to judge garbage
Adds a reference counter to the object , If you have a reference to this object, add... To the count 1, If the reference fails, it will be reduced 1, The counter at any time is 0 The object of the judgment is garbage .
- advantage ： Implement a simple , Efficient .
- shortcoming ： It can't solve the problem of object circular reference . In multithreaded environment , Reference count changes are also expensive to synchronize , Low performance .
The current mainstream algorithm of virtual machine . He's from the root node （GC Root） Search down for the object node , The search path is called the reference chain , When there is no connection between an object and the root node , This object is judged as garbage .
GC Root object
It can be used as GC Roots The objects of include ：
- Objects referenced in the virtual machine stack
- Method area class static attribute credit object
- Method to remove objects referenced by constants
- Local method stack JNI（Native Method ） Referenced object
Judging the survival of an object is related to reference ,jdk1.2 Before , Reference only refers to the address of another memory block stored in the data of reference type .jdk1.2 after ,java There are four types of references to .
- Strong citation ：Object obj = new Object(); Most common references , This way new Create a strong reference to the object . As long as the object has a strong reference , also GC Roots Can be up to , Then the object is not recycled .
- Soft citation : SotfReference Class to achieve . Represents some useful but unnecessary objects . stay OOM front , The garbage collector will include the objects pointed to by these soft references in the collection scope , For objects associated with soft references , Recycle only when memory is low .
- Weak reference ：WeakReference, Represents an unnecessary object , stay YGC When it comes to recycling . because YGC Time is uncertain , So weak references can be recycled at any time .
- Virtual reference ：PhantomReference, Cannot get the object pointed to through this reference . It can be collected by the garbage collector at any time . Virtual references are mainly used to track the activities of objects recycled by the garbage collector , Must be used with reference queues :
ReferenceQueue<String> queue = new ReferenceQueue<>(); PhantomReference<String> phantomReference = new PhantomReference<>("Hello", queue);
When the garbage collector is ready to recycle an object , I found that he had a false quote , Before reclaiming the object's memory , Queue this virtual reference , The program checks whether there is the virtual reference in the reference queue , To see if the object is going to be recycled by the garbage collector , Then you can do something before recycling .
Garbage collection algorithm
Mark - eliminate （Mark-Sweep）
The most basic algorithm , First step , Mark... First , from GC Roots set out , Mark object references in turn , then , Clear the unmarked objects .
shortcoming ： One is efficiency , The efficiency of both marking and clearing processes is not high ; in addition , After cleaning, a large number of discontinuous memory fragments will be generated . When we need to allocate large objects , Unable to find enough contiguous memory to trigger again GC.
Copy algorithm ： Divide memory into two areas of the same size , Use one piece at a time , When it's finished , Just copy the surviving objects in one area to another , And then remove this piece . No memory fragmentation , But memory utilization is not high , Half of the space is wasted .
Most of the objects in the new generation “ transient ”,HotSpot By default, the new generation memory is divided into a large Eden Area and two small Surivor District . stay GC when , take Eden District and a Surivor The living objects in the zone , Copy to another Surivor District .Eden Area and two Surivor The default area ratio is 8：1：1, So it's just a waste of time 10% Space .
Mark - Arrangement (Mark-Compact)
It's the same as mark removal , Mark... First , But we don't recycle objects directly afterwards , It's moving the living object to one end , Then directly clean up the memory outside the end boundary .
Because the replication algorithm in the survival of more objects , Low efficiency , And there's a waste of space , So in the old age, we usually choose the mark - Sorting algorithm .
### Three GC Algorithm comparison
|Mark - eliminate||Implement a simple||There are memory fragments|
|Copy||No debris , Good performance||Low memory usage|
|Mark - Arrangement||No debris||The sorting process is expensive|
Combine the above algorithms , Adjust measures to local conditions . Generally, we divide the heap space into the Cenozoic and the old generation . According to their specific use of different garbage collection algorithm , In the new generation , Every time GC A lot of people die , A few survived , So we use the replication algorithm . It costs only a few objects to copy , Does not cause discontinuous memory fragmentation . And in the older generation , The survival rate is high , Mark up or clear up .
- STW: "Stop The World" Abbreviation , It's also called global pause . It will cause a suspension of service , No response .
- Serial collection ： GC Single threaded memory recovery , All user threads will be suspended . such as Serial, Serial Old
- Parallel collection : Multithreading for concurrency GC, This pauses the user thread . such as Parallel
- Concurrent collection ： User threads and GC Thread executing at the same time , There is no need to pause the user thread . It's suitable for scenarios with response time requirements . such as CMS The collector
Serial It's a single threaded collector , It only uses one CPU Or a collection thread to do garbage collection , And when doing garbage collection, you need to stop all worker threads , Until the end of the collection .Serial Stop the user thread , Using replication algorithm to collect young generation ,Serial Old Using tag - Sort out the algorithm to collect the older generation .
characteristic ： Single thread collection ,STW
Use -XX:+UseSerialGC Turn on Serial + Serial Old
Serial Multithreaded version of , Using multithreading for garbage collection . He's a new generation of garbage collectors . We need to cooperate with the old CMS Collector usage . So you need to use CMS, The new generation acquiesces to ParNew.
Can pass ==-XX:ParallelGCThreads== Parameter to control the number of threads collected , The process is also STW
Parallel Scavenge/Paraller Old
Parallel Scavenge It's a new generation of collectors , Using replication algorithm , Multithreaded collectors ,Paraller Old It's an old time collector , It's also multithreaded collection , Using tag - Sorting algorithm .
- -XX:UseParallelGC Turn on
- -XX:MaxGCPauseMillis Maximum garbage collection pause time
- -XX:GCTimeRatio Set throughput size
Through parameters -XX:MaxGCPauseMillis Control maximum GC Pause time .-XX:GCTimeRatio Set throughput size
Increasing throughput can make efficient use of CPU Time , Finish the program as soon as possible .
The adaptive GC Strategy
Processing provides the above two parameters to control the throughput size ,Parallel Scavenge You can also use -XX:+UseAdptiveSizePolicy, Turn on the indicator GC Strategy . After opening , You don't need to set the Cenozoic size manually ,Eden/Surivor Scale and other parameters , The virtual opportunity is based on the operation of the system , Adjust these parameters dynamically , So as to achieve the optimal pause time , And maximum throughput .
- 1. Initial marker ： Mark only GC Roots Objects that can be directly related to , Meeting STW.
- 2. Concurrent Tags ： Conduct GC Roots Tracing The process of ,GC Thread and user thread execute at the same time .
- 3. Re label ： Fix concurrency flag period , Objects whose tags change as a result of the program running . Meeting STW
- 4. Concurrent elimination ： Concurrent garbage collection （GC Thread and user thread execute at the same time ）
- 5. Concurrent reset ： Clean up this time CMS GC Context information , For the next time GC To prepare for .
advantage ： Low pause , Concurrent execution
- Because of concurrent execution , So for CPU There's a lot of pressure on resources .
- Unable to handle floating garbage generated during collection .
- Because of the use of markers - Clear algorithm , So it's a lot of memory fragmentation . And the reason why we need to allocate large objects is insufficient memory , Trigger FullGC.
Use -XX:UseConcMarkSweepGC Turn on ParNew+CMS/SerialOld Collector assembly , That is, the new generation adopts ParNew, Old age CMS, When CMS When something goes wrong ,SerialOld spare .
To solve the problem of memory fragmentation ,CMS Can pass -XX:+UseCMSCompactAtFullCollection, mandatory JVM stay FullGC After that, we compress the older generation , Perform defragmentation , At the same time STW. Want to reduce STW frequency , You can configure the -XX:+CMSFullGCsBeforeCompaction Parameters , After the number of times the setting is executed ,JVM And then in the older generation of space arrangement .
JDK9 Have already put CMS Mark as obsolete , stay JDK14 The CMS Delete .
JDK7 A new generation of collectors ,
Is a collector for server applications . Compared to the collector above ,
G1 Acting on the whole reactor , Other collectors only work on the new generation or the old generation .
G1 take Java Heap Split into pieces of the same size Region, Through parameters -XX：G1HeapRegionSize Appoint Region Size , The value range is 1～32M, Should be 2 Of N The next power .G1 For each Region I've classified it , Include respectively ：Eden,Surivor,Old, Humongous, among Humongous It's equivalent to a big Old, Used to store magnified objects .
G1 Divide the space into multiple areas , Track the value of the garbage in each area , Build a priority list , Give priority to areas with the most garbage collection , That's why it's called Garbage-First Why .
G1 And CMS Compared with , What are the characteristics?
- Concurrency and parallelism ： Make the most of CPU, To shorten the STW Time . The concurrent marking phase can be executed concurrently with the user thread , Final marking stage GC Threads can execute in parallel .
- Generational collection ：G1 Can not cooperate with other garbage collectors , Complete the whole... Independently GC Garbage collection .
- Spatial integration ：G1 On the whole, it adopts “ Mark - Arrangement ” algorithmic , There are also some parts Eden and Surivor Region It seems to adopt “ Copy ” Algorithm to achieve . The whole process avoids memory fragmentation .
- The pause time can be controlled ：G1 In addition to the pursuit of low pause , It can also establish a predictable pause time model , It allows users to specify a period of time , The time spent on collection is no more than one period of time .
G1 Garbage collection mode
- 1. all Eden Region When it's all full , It will trigger Young GC
- 2.Eden Region The objects in will be transferred to Surivor Region
- 3. primary Surivor Region Transfer objects in to another Surivor, Or promoted to Old Region
- 4. Free Region Will be put on the free list , Waiting for the next use .
When the elderly take over the whole Heap The size percentage of reaches a threshold （-XX:InitialingHeapOccupancyPercent） when , Default 45%, It will trigger Mixed GC,
Collect the whole new generation and part of the old age .
Mixed GC Recycling process
- 1. Initial marker ： Mark only GC Roots Objects that can be associated with . modify TAMS Value , This stage will STW.
- 2. Concurrent Tags : from GC Root Start reachability analysis of objects in heap memory , Find out who's alive , This process can be executed concurrently with the user thread .
- 3. Final marker ： Fix the record that the user thread continues to run during the concurrent marking phase, causing the marking to change . This process STW, But it can be executed in parallel .
- 4. Screening and recovery ： To each Region Sorted by recovery value and cost , Make recycling plan according to the pause time expected by users .
When there is not enough memory for the copied object , Or can't allocate enough space , Trigger Full GC
Full GC The model is to use Serial Old Collected , So will STW.
How to reduce Full GC？
- increase -XX:G1ReserverPercent, To increase the reserved memory .
- Reduce -XX:InitialingHeapOccupancyPercent, When the older generation reaches this value, it triggers Mixed GC,
- increase -XX:ConcGCThreads The number of threads in the concurrent phase .
Summary of the above garbage collector
- Serial Serial , For the new generation , Using replication algorithm
- ParNew parallel , For the new generation , Using replication algorithm
- Serial Old Serial , For the old age , Mark - Sorting algorithm
- Parallel parallel , For the new generation , Copy algorithm
- Parallel Old parallel , For the old age , Mark - Sorting algorithm
- CMS Concurrent , For the old age , Mark - Clear algorithm
- G1 Concurrent + parallel , Acting on the whole stack , Copy algorithm , Mark - Arrangement
《 In depth understanding of Java virtual machine JVM Advanced features and best practices 》