This article is an in-depth understanding of the Java Virtual Machine JVM advanced features and best practices book notes
Before introducing the Java garbage collection method, let's look at what the Java Virtual machine divides the memory it manages into several different data areas during the execution of a Java program.
Partition of the 1.Java runtime data area
Such as:
Where the program counter, virtual machine stack, the local method stack, the memory of the 3 regions of the thread, and out of the thread, so the memory allocation and recycling of these areas are determined, we do not need to consider the memory allocation and recycling of these areas. The heap and the method area are different, we can only know when the program is running, which objects will be created, this part of the memory allocation and recycling is dynamic, the garbage collector is concerned about this part of memory (heap and method area).
Let's start with a brief introduction of what is stored in these parts of the area.
- Program counter:(thread private) the line number indicator of the byte code executed by the current thread, and the interpreter works by changing the value of the counter to get the next instruction to be executed.
- java Virtual machine stack :(Thread-private) describes the memory model that is executed by the Java method, each of which creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method is called from the tick until the execution completes, corresponding to the stack frame in the virtual machine stack in the stack and the process of the stack. This is what we used to say about stack memory.
- The local method stack :(Thread-private) is similar to the Java Virtual machine stack, except that it serves the native method used for virtual opportunities, and it throws Stackoverflowerror and OutOfMemoryError exceptions.
- java heap:(shared by all threads) Almost all object instances are allocated memory here, and Java heap can be subdivided into the new generation and the old age;
- method Area:(thread sharing) is used to store data such as class information, constants, static variables, instant compiler compiled code, etc. that have been loaded by the virtual machine, and (on some virtual machines, also known as permanent generations).
2 Java (JVM) garbage collection mechanism 2.1 What memory needs to be recycled?
In Java, the object survives by the accessibility analysis (if the object is dead, the memory it consumes is the one that needs to be recycled). The basic idea of the accessibility analysis algorithm is to start with a series of objects called "GC Roots", search down from these nodes, and search through the route called the reference chain. When an object does not appear on any of the reference chains, the object is judged to be unavailable (dead, recyclable).
Objects that are judged as unavailable by the accessibility analysis algorithm are not necessarily recycled, they also undergo a filtering process, and the criteria for filtering is whether this object is to execute the Finalize () method if the object does not overwrite the Finalize () method or its finalize () method is executed when the last garbage collector was working, it is determined that the Finalize () method is not executed (the object will be recycled in this collection), and if the Finalize () method is determined to be executed, the object is placed in a f-queue queue. Later, the virtual machine creates a finalizer thread (low priority) to trigger this method, but the VM does not promise to wait for it to execute the method. (That is, the object may be recycled when the Finalize () method is executed), and if the object is added to any of the reference chains in the Finalize () method, the object will not be recycled when the collector is working.
In Java, there are several reference objects that can be used as GC Roots objects: Virtual machine stacks (local variable tables in stack frames). A class static property in a method area and an object referenced by a constant and a jni reference in the local method stack;
2.2 Garbage Collection Algorithm 2.2.1 Tag-purge (Mark-sweep) algorithm
First, the previous accessibility analysis algorithm is used to mark the objects that need to be recycled, and when the tag is complete, all tagged objects are collected uniformly, and the disadvantages of this algorithm are mainly:
- efficiency problem , the efficiency is not high in marking and clearing two processes;
- space Problem , after the mark clears, it generates a lot of memory fragments, too many fragments, which may cause the garbage collection action to be triggered in advance when allocating memory for large objects.
2.2.2 Replication algorithm (coping)
Divide the available memory into two pieces, one at a time, so that you can only allocate memory sequentially, and when a piece of memory is exhausted, copy the surviving object to another memory and then recycle the used memory space. (generally not in the form of an average of two blocks, modern virtual machines typically divide memory into a larger Eden space and two smaller survivor space, each time using Eden space and a piece of survivor space, when recycled, Copy the Eden space and the surviving objects in the survivors space into another unused survivor space, and then clean up the used space, and this algorithm will generally reclaim the new generation of memory space;
2.2.3 Labeling-Finishing (mark-compact) algorithm
Use the accessibility analysis algorithm to mark the objects that need to be recycled, and then let the surviving objects (objects appearing in any reference chain) move toward one end, and then clean out the memory outside the end boundary. (commonly used to recover objects from the old age);
3 When to recycle
In most cases, objects are allocated first in the Eden area (large objects are allocated directly in the old age), and when Eden does not have enough space, the JVM initiates a minor GC. Before minor GC, the JVM would check that the largest available space in the old age was greater than the space of all the new generation objects, and if so, the minor GC would be safe, otherwise the JVM would check if the Handlepromotionfailure set value allowed the warranty to fail. If the guarantee fails, it will continue to check if the maximum free space in the old age is greater than the average size of the previous promotion to the old age object, and if so, attempt to minor GC (if it fails, a full GC will be performed), or a full GC will be performed instead;
Java garbage collection and memory allocation policy