Interview summary Java garbage collection

First, the so-called GC

GC is a garbage collection means (Gabage Collection).

We will be created in the development of many objects that are thrown into the pile will fall if these objects only increase not decrease, then the heap space will soon be exhausted. So we need to clean out some useless objects.

Second, the object is dead right

If the referenced object is set to null, if the garbage collector will immediately release the memory occupied by the object?
Not. Object recovery is a process, this process can revive the object. And garbage collection is uncertain, Zhibuding when to start recovery.

Java in the death of the subject is determined in two ways:
reference counting
reachability analysis
See: https://blog.csdn.net/weixin_44026997/article/details/104312276

Third, the method of recovery zone

The method area is not within the stack, it will be garbage?

In jdk1.7, method area permanent generation permanently substituting itself is a product of the concept of garbage recycling, the recovery method will be full gc time zone.

To jdk1.8, although permanent behalf was canceled, but added MaxMetaspaceSize parameters, for garbage collection when the dying classes and class loader will be used to reach the set value "MaxMetaspaceSize" parameter in the metadata.

Therefore, the method area will be recycled.

Fourth, the garbage collection algorithm

Mark - sweep algorithm
replication algorithm
mark - Collation Algorithm
generational collection algorithm

4.1 mark - sweep algorithm
marks - Clear considered the most basic collection algorithm. Its idea is to first mark, then clear. An object to be recycled, is at least two markers.
Here Insert Picture Description

It has two main disadvantages:

Not efficient
will produce large amounts of memory fragmentation
memory fragmentation is referred to as memory space is relatively fragmented, lack of contiguous space large segments. So if suddenly a large object, will not find a large enough contiguous space to store, so shall no longer be triggered once gc.
Here Insert Picture Description
4.2 copy algorithm
thought that the replication algorithm, the memory is divided into two, divided into two regions is assumed that A, B it.

After each subject, and they all put in area A, when the A area is full, the copying surviving objects to the B area, and then empty the A area.

The next object is placed in the entire region B, region B is full, etc., duplicate object to put the survival region A, region B and then emptied.

In this way and shift back and forth to complete garbage collection.

The advantage is no space debris, the disadvantage is that each only get half the memory.

The disadvantage is that at the higher survival rate of the object scene (such as the environment years old) needs to be copied too many things, efficiency falls.

Here Insert Picture Description
4.3 mark - Collation Algorithm
mark - "mark" phase and finishing algorithm "mark - clean-up" of the same mark. The difference is that death does not directly target clean-up, but put them all together to move in memory, and then clean up together.
Here Insert Picture Description
4.4 generational collection algorithm
generational collection algorithm is actually nothing new, but the survival rate by blocking the object, and then choose the appropriate collection algorithm.

java is used in generational collection algorithm.

Low survival rate objects together, called the young generation, using the copy algorithm to collect.

High survival rate objects together, known as the old era, the use of mark - sweep or mark - sorting algorithms.

1). New Generation (Young Generation)

New Generation goal is to collect as quick off the short life cycle of those objects, under normal circumstances, all newly generated objects are on the first of the new generation. The new generation memory in accordance with 8: 1: 1 ratio into a eden region and two survivor (survivor0, survivor1) area, most of the objects generated in the Eden area. During garbage collection, the first district eden live objects copied to survivor0 area, then emptied eden zone, when the survivor0 area is also full, then the eden zone area and survivor0 survival copy objects to survivor1 area, then emptied and the eden survivor0 area, this time survivor0 area is empty, then switch roles survivor0 area and survivor1 area (ie, the next garbage will scan the Eden area and survivor1 zone recovery), that is, keeping survivor0 area is empty, and so forth. In particular, when survivor1 storage area is not enough to live objects eden region and survivor0 area, it will be live objects placed directly on to the old era. If the old year was full, it will trigger a FullGC, is the new generation, the old year are recycled. Note, GC Cenozoic is also called MinorGC, MinorGC frequency of occurrence is relatively high, and so is not necessarily the only trigger Eden area is full.

2) Old's (Old Generation)

Old's store are some of the longer life cycle of the object, as described above, as the object went through N times after garbage collection is still alive in the new generation will be placed in the old era. In addition, the memory of old age is also much larger than the new generation (roughly the ratio is 1: 2), when the old year full trigger Major GC (Full GC), the object's old survived a long time, so the frequency of occurrence of relatively low FullGC .

3) Permanent Generation (Permanent Generation)

Mainly used for storage of permanent generation of static files, such as Java classes and methods. When the permanent generation of no significant impact on garbage collection, but some applications may be dynamically generated, or call some class, such as the use of a reflective, dynamic agency, CGLib and other bytecode framework, at this time need to set a relatively large permanent generation of space to store the run during the new class.

Published 114 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44026997/article/details/105164687