当前位置:网站首页>Chapter 27 "JVM judge object is dead, practice verification GC recovery"

Chapter 27 "JVM judge object is dead, practice verification GC recovery"

2021-01-23 18:27:52 bugstack_ cn

author : Little brother Fu
Blog :https://bugstack.cn

precipitation 、 Share 、 grow up , Let yourself and others have something to gain !

One 、 Preface

How important it is to improve your value ?

After all the ups and downs , I've seen men and women . There is no immutable time passing by !

Someone got off on this train 、 Someone's on it , The label that others give you to comment on 、 The brand left behind , It's just a story from this train . Only individuals grow up 、 accumulated 、 The precipitation , To be your own driver .

Maybe you don't understand at some age , But if one day you're not that busy , Think about your own way 、 My own steps . See if these are what you want , If it's all you want , Why do you look unhappy ?

good ! come on. , Go to who you want to be !

Two 、 Interview questions

Thank you plane , Notes !, At noon, I am full and in a daze , Why can't we learn this knowledge , It doesn't go into the brain either !

Thank you plane : feed , Big brother of the interviewer , I want to ask a question .

interviewer : what ?

Thank you plane : It's the knowledge that doesn't go into the brain !

interviewer : this ….

Thank you plane : It's just that I've read and forgotten , I forgot to see !

interviewer : Is there no practice ? I just saw it and thought it would , Collection means understanding ? It's not going anywhere !?

Thank you plane : Seems to be ! What can I do ?

interviewer : There's no good way , Learning itself is a boring thing . Reduce the time waste of fragmentation , It's better to use it more in systematic learning . Even if you blog down , Verification is also good .

3、 ... and 、 Start by verifying garbage collection

It's garbage collection , If I don't quote it, I recycle it ? When was it recycled ? How to recycle ?

I don't see any real examples , It is often difficult for science students to accept this kind of knowledge . I'm the same myself , It's better for me to see . Code is the concrete realization of mathematical logic , It doesn't make sense to just look at the answer without the implementation process .

Test code

public class ReferenceCountingGC {

    public Object instance = null;
    private static final int _1MB = 1024 * 1024;
    /**
     *  The only meaning of this member property is to occupy memory ,  In order to be able to GC Check the log to see if there is any recycling 
     */
    private byte[] bigSize = new byte[2 * _1MB];

    public static void main(String[] args) {
        testGC();
    }

    public static void testGC() {
        ReferenceCountingGC objA = new ReferenceCountingGC();
        ReferenceCountingGC objB = new ReferenceCountingGC();
        objA.instance = objB;
        objB.instance = objA;
        objA = null;
        objB = null;
        //  Suppose it happens in this line GC, objA and objB Can it be recycled ?
        System.gc();
    }

}

The example comes from 《 In depth understanding of Java virtual machine 》 The counting algorithm chapter is cited in .

The result of the example is , But it has been set to null Two objects of , Will it be GC Recycling . If it's just based on the reference counter algorithm , Then the count identifier of these two objects will not be 0, It can't be recycled . But is it recycled or not ?

Here we start with jvm Tool instructions ,jstat To monitor . Because I need to tap the code in the process of monitoring , More time-consuming , So we're calling testGC() front , Sleep meeting Thread.sleep(55000);. After starting the code, execute the following command .

E:\itstack\git\github.com\interview>jps -l
10656
88464
38372 org.itstack.interview.ReferenceCountingGC
26552 sun.tools.jps.Jps
110056 org.jetbrains.jps.cmdline.Launcher

E:\itstack\git\github.com\interview>jstat -gc 38372 2000
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0    0.0   65536.0   6561.4   175104.0     0.0     4480.0 770.9  384.0   75.9       0    0.000   0      0.000    0.000
10752.0 10752.0  0.0   1288.0 65536.0    0.0     175104.0     8.0     4864.0 3982.6 512.0  440.5       1    0.003   1      0.000    0.003
10752.0 10752.0  0.0    0.0   65536.0   437.3    175104.0    1125.5   4864.0 3982.6 512.0  440.5       1    0.003   1      0.012    0.015
10752.0 10752.0  0.0    0.0   65536.0   437.3    175104.0    1125.5   4864.0 3982.6 512.0  440.5       1    0.003   1      0.012    0.015
  • S0C、S1C, The size of the first and second survival zones
  • S0U、S1U, The first and second surviving areas use size
  • EC、EU, The size and use of Eden
  • OC、OU, The size and use of the old age
  • MC、MU, The size and use of the method area
  • CCSC、CCSU, Compression class space size and use
  • YGC、YGCT, The number and time of garbage collection of young generation
  • FGC、FGCT, The number and time of garbage collection in the old age
  • GCT, Garbage collection takes time

Be careful : Look at the back three lines ,S1U = 1288.0GCT = 0.003, Indicates that garbage collection is already in progress .

Next , Let's test it another way . In the startup program , Join in GC Printing parameters , Observe GC The result of the change .

-XX:+PrintGCDetails   Print every time gc The recovery of   Print heap space and memory information after program running ( Including memory overflow )
-XX:+PrintHeapAtGC   Print every time gc Memory before and after 
-XX:+PrintGCTimeStamps  Print every time gc The time stamp of the interval  full gc For each new generation of old age and the whole space to do a unified recycling   We should try to avoid 
-XX:+TraceClassLoading   Print class loading 
-XX:+PrintClassHistogram  Print the memory footprint of each instance of the class 
-Xloggc:/Users/xiaofuge/Desktop/logs/log.log   With the above use, print the above log to the specified file 
-XXHeapDumpOnOutOfMemoryError  When a memory overflow occurs, the heap information is transferred to storage   For analysis 

This time you can get rid of sleep , And add parameters -XX:+PrintGCDetails, as follows :

test result

[GC (System.gc()) [PSYoungGen: 9346K->936K(76288K)] 9346K->944K(251392K), 0.0008518 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 936K->0K(76288K)] [ParOldGen: 8K->764K(175104K)] 944K->764K(251392K), [Metaspace: 3405K->3405K(1056768K)], 0.0040034 secs] [Times: user=0.08 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 76288K, used 1966K [0x000000076b500000, 0x0000000770a00000, 0x00000007c0000000)
  eden space 65536K, 3% used [0x000000076b500000,0x000000076b6eb9e0,0x000000076f500000)
  from space 10752K, 0% used [0x000000076f500000,0x000000076f500000,0x000000076ff80000)
  to   space 10752K, 0% used [0x000000076ff80000,0x000000076ff80000,0x0000000770a00000)
 ParOldGen       total 175104K, used 764K [0x00000006c1e00000, 0x00000006cc900000, 0x000000076b500000)
  object space 175104K, 0% used [0x00000006c1e00000,0x00000006c1ebf100,0x00000006cc900000)
 Metaspace       used 3449K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
  • From the running results, we can see that the memory recovery log ,Full GC It's recycled .
  • It can also be seen that JVM It doesn't depend on the way the counter is referenced , Judge whether the object is alive . Otherwise they won't be recycled

So here's an example , Let's go on to see JVM The knowledge framework of garbage collection !

Four 、JVM Garbage collection knowledge framework

garbage collection (Garbage Collection, abbreviation GC), As early as 1960 Born in Massachusetts Institute of technology Lisp It was the first language to use dynamic memory allocation and garbage collection .

There are three main things garbage collectors do : What memory needs to be recycled When to recycle 、 How to recycle .

And from the birth of the garbage collector to now there is half a century of development , Now the dynamic memory allocation and memory recovery technology has been very mature , Everything seems to have entered “ automation ”. But sometimes we still need to monitor in high concurrency scenarios , Whether there is a memory overflow 、 leak 、GC Time course and so on . Therefore, it is very important for senior programmers to understand and know the relevant knowledge of garbage collection .

The core knowledge items of garbage collector mainly include : Judge whether the object is alive 、 Garbage collection algorithm 、 All kinds of garbage collectors and garbage collection processes . Here's the picture ;

 chart  27-1  Garbage collector knowledge framework

Download link to the original picture :http://book.bugstack.cn/#s/6jJp2icA

1. Judgment object is dead

1.1 Reference counter

  1. Add a reference counter for each object , Count the number of references to the object .
  2. When an object has a corresponding reference update operation , Increase or decrease the reference counter of the target object .
  3. Once the reference counter of an object is 0 when , The object is dead , It can be recycled by garbage .

In terms of implementation , Reference counter method (Reference Counting) Although it takes up some extra memory space to count , But its implementation is simple , High judgment efficiency , It's a good algorithm .

There are also some well-known cited cases , such as : Microsoft COM(Component Object Model) technology 、 Use ActionScript 3 Of FlashPlayer、 Python Language, etc. .

however , In the mainstream Java Virtual machine does not use reference technology algorithm to manage memory , The main reason is that this simple counting method is dealing with some interdependencies 、 Circular references and so on can be very complicated . There may be memory that is no longer in use but cannot be recycled , Memory leak

1.2 Accessibility analysis

Java、C# And other mainstream language memory management subsystem , All through accessibility analysis (Reachability Analysis) Algorithm to determine whether the object is alive .

Its algorithmic idea is to define a series of GC Roots The root object serves as the starting node set , Starting from these nodes , Enumerate all the objects that the Collection refers to, and fill in the collection (live set). This process taught marking , Mark only the surviving objects good , Now, unmarked objects are objects that can be recycled .

GC Roots Include ;

  1. Global references , For static objects in the method area 、 References to constant objects
  2. Execution context , Yes Java Local object reference in method stack frame 、 Yes JNI handles Object reference
  3. Started and not stopped Java Threads

Two major problems

  1. False positives : Dead objects are marked alive , Garbage can't be collected . Take up a little more memory , The impact is small .
  2. Omission of : Referenced object ( In use ) Not marked as alive , It's recycled . So the direct result is JVM Run away .(STW It can ensure the accuracy of accessibility analysis , Avoid missing reports )

2. Garbage collection algorithm

2.1 Mark - Clear algorithm (mark-sweep)

 Mark - Clear algorithm (mark-sweep)

  • Mark free memory occupied by dead objects without references , And record it in the free list (free list).
  • When you need to create a new object , The memory management module will run from free list Search for free memory in , Assigned to a new object .
  • This cleaning method is actually very simple and efficient , But there is also a problem, memory fragmentation is too serious .
  • Java Objects in the heap of the virtual machine , It has to be continuously distributed , So in extreme cases, even if the total remaining memory is sufficient , But looking for continuous memory allocation is inefficient , Or too serious to allocate memory . Restart Tom Cat !
  • stay CMS There are applications of this kind of algorithm in ,GC The pause is short , But there are algorithmic flaws .

2.2 Mark - Copy algorithm (mark-copy)

 Mark - Copy algorithm (mark-copy)

  • From the picture, this time after finishing garbage cleaning, the continuous memory space is large .
  • In this way, the memory area is divided into two parts , Use two Pointers from and to maintain , And only use from The memory area that the pointer points to allocates memory .
  • When garbage collection occurs , Copy the surviving object to to The area of memory to which the pointer points , And exchange from And to The pointer .
  • Its benefits are obvious , Is to solve the problem of memory fragmentation . But there are other problems as well , Half of the heap space is wasted .

2.3 Mark - Compression algorithm (mark-compact)

 Mark - Compression algorithm (mark-compact)

  • 1974 year ,Edward Lueders Put forward the mark - Compression algorithm , The process of tagging is the same as the tag clearing algorithm , But in subsequent object cleaning steps , First, move the surviving objects to one end of the memory space , And then clean up other memory space .
  • This algorithm can solve the problem of memory fragmentation , But the performance cost of compression algorithm is not small .

3. Garbage collector

3.1 The new generation

  1. Serial
    1. Algorithm : Mark - Copy algorithm
    2. explain : Simple and efficient single core machines ,Client The default new generation collector in mode ;
  2. Parallel ParNew
    1. Algorithm : Mark - Copy algorithm
    2. explain :GC Thread parallel version , In the single CPU The scene effect is not outstanding . Commonly used in Client Mode of JVM
  3. Parallel Scavenge
    1. Algorithm : Mark - Copy algorithm
    2. explain : The goal is to achieve controllable throughput ( throughput = User code run time /( User code run time + Garbage collection time ));

3.2 Old age

  1. Serial Old
    1. Algorithm : Mark - Compression algorithm
    2. explain : Average performance , Single threaded version .1.5 Before and Parallel Scavenge In combination with ; As CMS The backup plan for .
  2. Parallel Old
    1. Algorithm : Mark - Compression algorithm
    2. explain :GC Multithreading parallel , To replace Serial Old And Parallel Scavenge In combination with .
  3. CMS
    1. Algorithm : Mark - Clear algorithm
    2. explain : Yes CPU Resource sensitive 、 A long pause . Mark - Clear algorithm , Memory fragmentation will occur , You can use parameters to enable the merging and collation of fragments . It has been basically G1 replace

3.3 G1

  1. Algorithm : Mark - Compression algorithm
  2. explain : It is suitable for multi-core and large memory machines 、GC Multithreaded parallel execution , Low pause 、 High recovery efficiency .

5、 ... and 、 summary

  • JVM There is a lot of knowledge about automatic memory management , Including those not mentioned in this article HotSpot Knowledge of implementation algorithm details , Include : Security nodes 、 The safety area 、 Card table 、 Write barriers, etc . Every content is worth learning .
  • If it's not just for the interview , The best way is to practice and verify learning . Otherwise, this kind of knowledge is like 3 It's the same as a movie , It's hard to remember its content .
  • The whole content is also a process of Xiao Fu GE's study and arrangement , We will continue to dig and share in the future . Interested partners can discuss and learn together .

6、 ... and 、 Series recommendation

( Please indicate the author and source of this article WeChat official account :bugstack Wormhole stack | author : Little brother Fu

Show Disqus Comments

版权声明
本文为[bugstack_ cn]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123182708186L.html

随机推荐