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

01 JVM memory composition structure

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

image.png

02 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)

image.png


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.

03 suggestions on JVM memory management

(1) manually generated undesired objects, the object is set to an intermediate null, to 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 solve, but must to go through a long-term testing of the physical machine, because different parameters may lead to different results. The -Xnoclassgc parameters.

04 garbage object determination

Java heap before storing almost all the instances of objects, the garbage collector of objects in the heap for recycling, should determine whether these objects have used, whether the object is determined with the following algorithm garbage objects

1. The 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.

2. The 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.

05 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.

1. 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:

image.png

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.

2. Copy the algorithm

复制算法比较适合于新生代,复制算法是针对标记—清除算法的缺点,在其基础上进行改进而得到的,它讲课用内存按容量分为大小相等的两块,每次只使用其中的一块,当这一块的内存用完了,就将还存活着的对象复制到另外一块内存上面,然后再把已使用过的内存空间一次清理掉。复制算法有如下优点:

  • 每次只对一块内存进行回收,运行高效。

  • 只需移动栈顶指针,按顺序分配内存即可,实现简单。

  • 内存回收时不用考虑内存碎片的出现。

它的缺点是:可一次性分配的最大内存缩小了一半。 复制算法的执行情况如下图所示:

image.png

但一般不用按1:1划分内存空间,可以分成一个大的eden和两块小的survivor。

3.标记—整理算法

老年代中,对象存活率比较高,如果执行较多的复制操作,效率将会变低,所以老年代一般会选用其他算法,如标记—整理算法。该算法标记的过程与标记—清除算法中的标记过程一样,但对标记后出的垃圾对象的处理情况有所不同,它不是直接对可回收对象进行清理,而是让所有的对象都向一端移动,然后直接清理掉端边界以外的内存。标记—整理算法的回收情况如下所示:

image.png


4.分代收集

当前商业虚拟机的垃圾收集都采用分代收集来管理内存,它根据对象的存活周期的不同将内存划分为几块,一般是把Java堆分为新生代和老年代。在新生代中,每次垃圾收集时都会发现有大量对象死去,只有少量存活,因此可选用复制算法来完成收集,而老年代中因为对象存活率高、没有额外空间对它进行分配担保,就必须使用标记—清除算法或标记—整理算法来进行回收。 每个对象都有一个年龄(Age)计数器,如果对象在Eden出声并讲过一次Minor GC还存活,将被移动到Survivor区并将Age设置为1,之后每在Survivor区中熬过一次Minor GC,Age就加1,当增加到一定程度(默认为15),就可以放到老年代中。

06 垃圾收集器

垃圾收集器是内存回收算法的具体实现,Java虚拟机规范中对垃圾收集器应该如何实现并没有任何规定,因此不同厂商、不同版本的虚拟机所提供的垃圾收集器都可能会有很大的差别。Sun  HotSpot虚拟机1.6版包含了如下收集器:Serial、ParNew、Parallel Scavenge、CMS、Serial Old、Parallel Old。这些收集器以不同的组合形式配合工作来完成不同分代区的垃圾收集工作。

07 垃圾回收分析

在用代码分析之前,我们对内存的分配策略明确以下三点:

  • 对象优先在Eden分配。当Eden没有足够空间分配时,将发起一次Minor GC

  • 大对象(需要大量连续空间的java对象,如长的字符串和数组)直接进入老年代。由于新生代使用复制算法回收内存,这样可以避免在Eden和两个Survivor区之间发生大量的内存复制。

  • 长期存活的对象将进入老年代。

对垃圾回收策略说明以下两点:

  • 新生代GC(Minor GC):发生在新生代的垃圾收集动作,因为Java对象大多都具有朝生夕灭的特性,因此Minor GC非常频繁,一般回收速度也比较快。

  • 老年代GC(Major GC/Full GC):发生在老年代的GC,出现了Major GC,经常会伴随至少一次Minor GC。由于老年代中的对象生命周期比较长,因此Major GC并不频繁,一般都是等待老年代满了后才进行Full GC,而且其速度一般会比Minor GC慢10倍以上。另外,如果分配了Direct Memory,在老年代中进行Full GC时,会顺便清理掉Direct Memory中的废弃对象。

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 blog.51cto.com/14453419/2421041