Ali asked the interviewer love memory management and GC algorithm and recovery strategy

JVM memory composition structure

JVM stack by the stack heap, stack, local methods, method area portion composed of the structure shown in FIG follows:

JVM garbage collection

Sun's JVMGenerationalCollecting (garbage collection) principle is this: the subjects were divided into younger generations (Young) , the old generation (Tenured) , permanent generation (Perm) , subject to different life cycles using different algorithms. (Analysis based on the object life cycle)

1.Young (the young generation)

The young generation is divided into three zones. A Eden area, two Survivor areas. Most objects generated in the Eden area. When the Eden area is full, live objects are also copied to the Survivor areas (two in one), Survivor When this area is full, live objects in this area will be copied to another area Survivor, and go when the Survivor full time, copied from the first Survivor areas come alive at this time and objects are copied old district (Tenured. it should be noted, Survivor of the two zones is symmetrical, the relationship has not, so the same area It may exist copied from Eden over objects, and objects from a previous Survivor copied, and copied to the old area of ​​the object only go from the first Survivor over. Also, there is a Chief Survivor is empty.

2.Tenured (old generation)

The old generation storage objects surviving from the young generation. In general the old generation are stored in a longer lifetime of the object.

3.Perm (permanent generation)

Used to store static files , and now Java classes, methods, and so on. Permanent generation no significant effect on garbage collection, but some applications may call some or dynamically generated class, such as Hibernate, etc., at this time need to set a relatively large permanent generation space to store these processes run in the new class. Permanent generation size by -XX: MaxPermSize = settings.

For example: When generating objects in the program, the normal allocation of space objects in the young generation, if it is too large objects in the old generation may also be generated directly (It was observed that every time a program is running will generate a Shizhao space, this memory allocation will directly send and receive messages using the old generation). The young generation in space is allocated when finished will initiate garbage collection, most of the memory can be recovered, the surviving part of the memory is copied to the area from Survivor, after repeated later if the recovery is also assigned from memory area is completed, it will memory also occur recovered and copied to the remaining objects to regions. To wait until the area is also full, garbage collection will happen again and then surviving copy of the object to the old district.

Usually we say that the JVM garbage collection always refers to the recovery of heap memory, really only a heap of content is dynamic allocation of application, the above objects of the young generation and the old generation are referring to the JVM Heap space, and the permanent generation is the previously mentioned MethodArea, does not belong to Heap.

Some advice on JVM memory management

  1. Manually generated objects useless, intermediate objects set is null, the speed up memory reclamation.

  2. If the target cell technology resulting object is a reusable object, the properties of which are different only be considered subject to less generation target cell. If there are idle object is pulled from the pool to use, does not generate a new object, greatly improving the reusability of object.

  3. JVM tuning parameters to configure the JVM's garbage collection to improve the speed, if there is no memory leak occurs and the above two methods can not guarantee the JVM garbage collection, consider using JVM tuning ways to resolve, but we must go through entities the long-term test machine, because different parameters may lead to different results. The -Xnoclassgc parameters.

Garbage object determination

Java heap to store objects with almost all instances, the garbage collector of objects in the heap pre recovery, should determine whether these objects have used, determine whether the object is garbage objects have the following algorithm:

Reference counting algorithm

Add a reference to an object counter, whenever a reference to its place, the counter value is incremented by 1 when referring to the failure, the counter value is decremented by 1, the counter at any time are the object 0 is no longer being used.

Reference implementation of simple counting algorithm to determine the efficiency is also high, in most cases it is a good choice when the Java language did not choose this algorithm to garbage collection, mainly because it is very difficult to solve between objects the mutual circular reference problem.

Root search algorithm

** Java and C # ** are based on the root search algorithm to determine whether the object alive. The basic idea of ​​this algorithm is a series of object called "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed chain of references, when an object has no references to GC Roots when connected to the chain, as evidenced by the object is not available. In the Java language, as GC Roots of cash include the following categories:

  • Virtual Machine stack (Local Variable Table stack frame) in the object reference.
  • The method of the referenced object region class static properties.
  • Object literal reference methods zone.
  • Native method stack JNI (Native Method) reference objects.

In fact, at the root of the search algorithm, to really proclaim the death of an object, at least twice to go through the labeling process: if the object is found during the search for the root of the chain of references is not connected to GC Roots, then it will be the first mark and to conduct a screening filter condition is whether this object it is necessary to perform the finalize () method. When the object is not covered finalize () method, or a finalize () method has been invoked over the virtual machine, the virtual machine these two cases are deemed not necessary to perform. If the object is determined to be necessary to perform a finalize () method, the object will be placed in a queue named F-Queue, and later by the one automatically created by the virtual machine, low-priority thread Finalizer to perform the finalize () method. finalize () method is the last chance to escape death fate of the object (as an object finalize () method will be called automatically once the largest system), GC will later F-Queue object in a second small-scale mark, if you want to succeed in saving their own finalize () method, so long as the object finalize () method to re-reference any object on the chain can associate. And then if the object has not been linked to any references in the chain, then it would be recovered out.

Garbage collection algorithm

In addition to objects is determined after the garbage, garbage can be recycled. Here are some of the garbage collection algorithm, as a result of garbage collection algorithm involves a large number of procedural details, so here mainly to articulate the ideas of each method, and not to fine on the specific implementation of the algorithm.

Mark - sweep algorithm

Mark - sweep algorithm is the most basic collection algorithm, which is divided into "mark" and "clear" in two stages: First mark the desired object marked recovery after the completion of uniform recycling mark out all objects are labeled, it's process is actually in front of the root search algorithm determines that the marking process garbage objects. Mark - sweep algorithm implementation shown below:

The algorithm has the following disadvantages:

  • Marking and clearance process efficiency is not high.
  • Will produce a large number of discrete memory fragmentation mark after clearing space debris could cause too much, when the program needs to allocate a large object during subsequent runs can not find enough contiguous memory and had to trigger another garbage collection action.
Replication algorithm

Replication algorithm more suitable to the new generation , the copy algorithm is the marking - to clear disadvantage algorithm, on the basis of its improvement is obtained, it lectures with memory capacity is divided into equal size by two, each of which only one when this one runs out of memory, it will also copy objects to another alive a memory above, then put the memory space has been used once cleared away. Replication algorithm has the following advantages:

  • Every piece of memory only for recycling, efficient operation.
  • Simply move the stack pointer, in order to allocate memory, simple.
  • RAM memory fragmentation occurs regardless of the time of recovery.

The disadvantage is: the largest one-time memory allocation reduced by half. Implementation of copy algorithm as shown below:

But generally do not have a 1: 1 division of memory space can be divided into one large and two small eden survivor.

Mark - Collation Algorithm

In the old years , the survival rate is relatively high target, if you perform more copy operations, efficiency will be low, so the old year will generally use other algorithms, such as mark - sorting algorithms. The algorithm labeled with the process mark - Clear labeling process algorithm of the same, but the disposition of garbage out after labeling the object is different, it is not directly recyclable objects to clean up, but to all objects to the end move, and then clean out the memory directly outside the terminal boundary. Mark - recovery of sorting algorithms as follows:

Generational collection

The current garbage collection business virtual machine implements a generational collection to manage memory, which according to the different periods of survival object memory is divided into a few pieces, usually the Java heap into the new generation and the old era. In the new generation, a time when garbage collection will find a large number of dead objects, only a few survive, so you can use the copy algorithm to complete the collection, while the old era because of the high survival rate of the object, there is no extra space is allocated to its guarantee, it you must use the mark - sweep algorithm or mark - sorting algorithms to recover.

Each object has an age (Age) counter, and if the object spoken aloud in a Minor Eden the GC still alive, will be moved to Survivor areas and Age set to 1, after each time Minor survived in the region Survivor GC, Age is increased by one, to a certain extent when (the default is 15), you can put the old era.

The garbage collector

The garbage collector is the implementation of garbage collection algorithms, Java Virtual Machine specification of how the garbage collector should be implemented and there is no requirement, so different manufacturers, different versions of the virtual machine provided by the garbage collector may be of great difference. Sun HotSpot VM version 1.6 contains the following collectors: Serial, ParNew, Parallel Scavenge, CMS, Serial Old, Parallel Old. These collectors cooperate to different combinations to accomplish different generational garbage collection area.

Garbage collection analysis

Before the code analysis, we assign the policy to clear the memory of the following three points:

  • Objects priority allocation in Eden. When there is not enough space allocation Eden, will launch a Minor GC
  • Large Object (requires a lot of java object of continuous space, such as long strings and arrays) directly into the old era. Since the new generation of replication algorithm used to reclaim memory, large memory to avoid replication occurs between two Eden and Survivor areas.
  • Long-term survival of the object will enter the old era.

Description of garbage collection policy the following two points:

  • New Generation GC ( Minor GC ): occurs in the new generation garbage collection action, because most of Java objects have properties Chaosheng evening off, so Minor GC very frequently, usually recover relatively fast speed.
  • Years old GC ( Major GC / Full GC ): GC occurs in old age, there was Major GC, often accompanied by at least one Minor GC. As the old era of object life cycle is relatively long, so Major GC infrequently, usually Full GC waiting to be filled after years old, and its speed is generally more than 10 times slower than the Minor GC. In addition, if the distribution Direct Memory, Full GC carried out in the old era, the way would be cleared away Direct Memory of discarded objects.

Dalvik virtual machine using the Mark-Sweep algorithm to garbage collection. As the name suggests, Mark-Sweep garbage collection algorithm is as Mark and Sweep two stages. Which, Mark sets the stage roots (Root Set) from the start, recursively mark all currently referenced objects, and those responsible for the recovery phase Sweep object is not referenced. Before analyzing the Mark-Sweep algorithm Dalvik virtual machine, we first take a look under what circumstances would trigger GC.

Guess you like

Origin juejin.im/post/5d2e9d40f265da1b725c379e
Recommended