JVM learning (a) JMM and garbage collection algorithm

Personal blog project address

I hope to help point a star, give me a little star ✨


Often using Java, but the bottom was not very familiar with its use, as well as in times of StackOverFlow or OOM, did not find a reason to go and upset, so he began learning JVM.

Zhou Zhiming recently read that the great God to write the "in-depth understanding of JVM", he wrote that the concept is relatively deep, so after reading it again, again combined with the article on the network will be better understood with absorption.

First, let's look at the Java virtual machine memory model (Java Memory Model)


Java virtual machine memory model (JMM)

java runtime data area is divided into five areas:

JMM model

In which the virtual machine stack, native method stacks and program counters are private to the thread of, the Java heap and method area is the various threads of shared memory area .

The following outlines the role of these five areas:

  • Program Counter: is a small memory space, its role can be seen as an indicator byte-code line number of the current thread of execution. When you own interpreters work it is by changing the value of the counter to select the next instruction to be executed.

  • Java virtual machine stack: its life cycle with the same thread, each method will be executed when at the same time create a stack frame (Stack Frame), for storing local variables table, stack operation, the dynamic linker, information methods exports. Each method is called until the completion of the execution process, it corresponds to a stack frame in a virtual machine stack from the stack to stack.

  • Native method stacks: the virtual machine stack lock role is very similar, except that the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and the local stack method is to use a virtual machine to a Native Method service. (Native Method Interface call is a non java java code, for example, C or C ++)

  • Heap: is the largest piece of memory in the Java Virtual manage. Java heap is shared by all threads in a memory area is created when the virtual machine starts. This memory area for the sole purpose is to store an object instance. At the same time, the heap is the main area managed by the garbage collector, and therefore often also referred to as "GC heap" (Garbage Collected Heap).

Now collectors are basically adopted zoning collection algorithm, so the Java heap can be subdivided into: the old and the new generation's; and then there is little detailed Eden space, From Survivor space, To Survivor space.

  • The method area: the Java heap, as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, code, etc. after the instant compiler (JIT) data.

The garbage collector

What memory need to recycle?

Before the heap objects recovery, we must first determine what the object is invalid. These objects also need to make sure what "alive" and which has "died" of (that is no longer referenced by any route to the object).

There are two ways judgment:

  • Reference counting objects are added to a counter when the object is a reference to another object or a variable, the counter is incremented; when referring to the failure, the counter value is decremented by one; any time counter object is no longer being 0 in use.

  • GC Roots and all objects directly or indirectly linked to reachability analysis method is a valid object, and the object is not associated with GC Roots is invalid object. GC Roots means:

  • Java Virtual Machine stack (local variables table stack frame) referenced objects

  • Method region static property class object reference

  • Method zone object literal references

  • Native method stacks object referenced by the JNI (Nativce method).

Comparison between the two: reference counting method is simple, but it is difficult to solve the problem is circulated between each object reference. So common is the reachability analysis algorithm.

But unreachable in reachability analysis algorithm object, not a "must die", this time they will be temporarily in a "probation" stage, to really proclaim the death of an object, at least twice to go through the labeling process. Which will involve finalize () method and F-Queue queue, you want to understand children's shoes can specifically find relevant information to find out ~


Garbage collection algorithm

  • Mark - sweep algorithm most basic collection algorithms is the "mark - sweep" (Mark-Sweep) algorithm: two stages, first mark all objects need to be recovered, after the completion of uniform recycling mark out all the objects are marked.

    Also there are two drawbacks: the first is efficiency , marking and clearance process efficiency is not high; the second is the space issue , will generate a lot of discrete memory fragmentation mark after clearing space debris too much may cause the program unable to find enough contiguous memory needs to be allocated a large object during subsequent runs and had to trigger another garbage collection operation in advance.

    Mark - sweep algorithm

  • The copy algorithm to solve the efficiency problem, it by the available memory capacity is divided into two equal size, uses only one of them. When a copy of this memory is used up, it will be still alive object to another one above, then memory space has been used once and then clean out. Benefit is that as long as the move top of the heap pointer, you can allocate memory in order to achieve simple, efficient operation, but the cost of this algorithm is to reduce by half the memory, feeling a little more harm than good ...

Copying

I mentioned in the book, and now most of the Java Virtual Machine are using this collection algorithm to recover the new generation.

New generation objects 98% Chaosheng late death, it does not require a 1: 1 ratio by dividing the memory space, but the memory space into a larger and two smaller Eden Survivor spaces, each Eden-use and where a Survivor. When recycling, the Eden and Survivor also alive objects to copy another piece of disposable Survivor space, and finally clean out space Eden and just used the Survivor.

HotSpot VM default size ratio is Eden and Survivor 8: 1 , i.e. each new generation of the hollow space is 90% of the total memory capacity of the new generation (80% + 10%). Of course, we can not guarantee that every recovery are only more than 10% of live objects, when Survivor space is not enough, need to rely on old's allocation guarantee (Handle Promotion).

  • Mark - Collation Algorithm (Mark-Compact) copying collection algorithm, it is necessary to perform more copy operations at higher target survival, efficiency will be low. So do not use replication years old collection algorithm, but the use of tags to organize algorithm (Mark-Compact).

The labeling process and still "mark - sweep" algorithm the same, but the subsequent steps are not directly recycled objects to clean up, but to all surviving objects are moved to the end , then clean out the memory directly outside the boundary.

Mark-Compact

  • Generational collection algorithm "generational collection algorithm" (Generational Collectio), depending on the life cycle of the object memory is divided into a few pieces, usually the Java heap into the new generation and the old time, so that you can adopt the characteristics of each era the most appropriate collection algorithm. For example, in the new generation, each garbage collection will have a lot of dead objects, only a few survive, then copy the selection algorithm, only need to pay the cost of reproduction of a small amount of live objects to complete the collection. The old era, because of the high survival rate of the object, there is no extra space is allocated to its guarantee, you must use the "mark - clean-up" or "mark - finishing" algorithm for recycling.

The first article about the memory model first record, the next record garbage collector.

Guess you like

Origin juejin.im/post/5ce8f1676fb9a07ec63af01f