JVM garbage collection (under)

Then, after previous, introduced over JVM garbage objects identified need to be recovered, this one is for us to say how the JVM garbage collection.

First to introduce here 80/20 法则:

Only about 20% becomes 80% by manipulation of the situation. In other words: all variables, the most important of only 20%, while the remaining 80% accounted for the majority, but far lower than the range of control of "critical few."

The life cycle of Java objects also have to meet such a law, that the majority of Java objects survived only a short time, but a small part of Java objects that survive will survive for a long time.

Therefore, it also created the JVM 分代回收thought. Simply put, the heap space is divided into two generations, are called 新生代and 老年代. Used to store new generation of new objects. When the object is to stay alive long enough, it is moved to the old era.

This also allows the JVM to different generations use different recovery algorithms.

For the new generation, we're guessing most of Java objects survived only a short time, then it can often garbage collection algorithm uses a shorter time-consuming, so most of the garbage can be recycled out in the new generation.

For old time, we're guessing most of the garbage has been recovered in the new generation, and the object in the old era of big probability will continue to survive. When the real trigger for the recovery of old age, it means that this assumption is wrong, or stack space has been exhausted. In this case, JVM often need to do a whole heap scan, regardless of the cost will also be time-consuming. (Of course, modern garbage collectors are on the path of development concurrent collection to avoid this whole heap scan.)

So, let's take a look at exactly how the JVM heap division.

Heap division

As described above, the heap is divided into the JVM old and new generation's, which in turn is divided into a new generation of Eden region, and two Survivor areas the same size.

Generally speaking, when we call the new directive, it will draw in the Eden area of ​​memory as a storage object. Since the heap space is shared by the threads, thus directly where space is designated side needs to be synchronized. Otherwise, it will be possible for some accident two shared memory objects appear.

JVM solution is to apply in advance for each thread a contiguous heap space, and only allows each thread to create their own objects in the heap space requested, the heap space if the application is used up, then continue to apply, this is TLAB (Thread Local Allocation Buffer, the corresponding virtual machine parameters -XX: + UseTLAB, enabled by default).

At this time, if the operation involves locking thread, the thread pointers need to maintain two (possibly more practically, it is important also two), a starting point TLAB the spare memory location, pointing to the end of a TLAB .

The next new instruction pointer adder can directly (bump the pointer) is achieved, i.e., the spare memory location pointer points plus the number of bytes requested.

If the value of the pointer after the addition of free memory is still less than or equal to the pointer points to the end, it represents the allocation succeeds. Otherwise, TLAB did not have enough space to meet this new operation. This time, the current thread will need to re-apply for a new TLAB.

There are no circumstances that may arise can not apply it? Yes, this time it will trigger Minor GCa.

Minor GC

The so-called Minor GC, refers to:

When the Eden area space runs out, JVM will conduct a Minor GC, to collect the new generation of garbage. Surviving objects will be sent to Survivor areas.

Mentioned above, the new generation of a total of two Survivor areas, we were used to from and to refer to. Which points to Survivior area is empty.

When occurs Minor GC, live objects Eden area and from point to Survivor region is copied to the to Survivor region pointed to, and then swap from and to the pointer, to ensure Survivor areas once Minor GC at the time, to directed or empty.

Survivor JVM will record a total area of ​​each object is copied back and forth several times. If the number of times an object is copied to 15 (corresponding to the virtual machine parameters -XX: + MaxTenuringThreshold), then the object will be promoted (promote) to the old era.

In addition, if a single Survivor region has been occupied by 50% (corresponding to the virtual machine parameters -XX: TargetSurvivorRatio), then the object high copy number will be promoted to the old era.

All in all, occurs when the Minor GC, we applied the 标记 - 复制algorithm, will be promoted to the survival of the old Survivor target area to the old era, and then copy the rest of the live objects and live objects Eden area to another Survivor region. Ideally, Eden area Basic are dead, you will need to copy the data will be very small, so the use of this 标记 - 复制excellent effect of the algorithm.

Another benefit Minor GC is not a whole heap of garbage collection. However, it does have a problem that the old era of the new generation of object may reference objects. In other words, when the mark live objects, we need to scan the object of the old era. If the object has a reference to the new generation of object, then this will also be cited as GC Roots. As a result, it would not do a whole heap scan it?

In order to avoid scanning whole heap, JVM called the introduction of 卡表technology, there may be roughly marked the old year to the new generation of memory area referenced. Interested friends can go to learn more about the limited space here, not specifically described.

Full GC

When that happens Full GCthen? For different garbage collectors, Full GC trigger conditions may not be the same. HotSpot VM achieved by the serial GC point of view, the trigger conditions are:

When ready to trigger a Minor GC, if it is found before the statistics say the average size of Minor GC promotion of large remaining space than the current decade old, but will not trigger Minor GC turned trigger Full GC.

Because the HotSpot VM's GC in addition to the garbage collector can collect old CMS's alone, while the other GC will collect the entire heap, there is no need to prepare in advance a single Minor GC.

Garbage Collection

Based recovery in three ways: 清除, , 压缩, 复制let's look at one by one.

Remove

The so-called Clear, is the death of memory occupied by objects marked as free memory, and recorded in a free list. When you need to create a new object memory management module will find free memory from the free list and allocated to the newly created object.

The principle is very simple, but there are two drawbacks:

  1. Cause memory fragmentation. Because the JVM heap object must be a continuous distribution, and therefore may appear enough total free memory, but was unable to allocate extreme cases.
  2. Allocative efficiency is low. If it is a continuous memory space, then we can pointer adder (pointer bumping) assigned to do. For a free list, JVM you need to access the items one by one in the free list, to find new objects can be placed in free memory.

compression

The so-called compression is the surviving objects gathered to the starting position memory area, leaving a period of contiguous memory space.

This approach can solve the problem of memory fragmentation, but at the cost of performance overhead of compression algorithms, so the efficiency of the distribution problem is still not resolved.

copy

Called copy, the memory area is equally divided into two, respectively, with two pointers from and to be maintained, and only a pointer from the memory area to allocate memory. When garbage collection occurs, put live objects copied to the memory area pointer points to, and exchange content to from pointers and pointers.

This recovery the same way to solve the problem of memory fragmentation, but it is also very obvious shortcomings, namely the use of heap space efficiency is extremely low.

Specific garbage collector

For the 新生代garbage collector has three: Serial, Parallel Scavenge and Parallel New. These three are based on the 标记 - 复制algorithm.

Which, it is a single-threaded Serial, Parallel New can be seen as a multi-threaded version of Serial, Parallel Scavenge and Parallel New similar, but more emphasis on throughput. In addition, Parallel Scavenge not be used with CMS.

For the 老年代garbage collector also has three: Serial Old, Parallel Old and CMS.

Serial Old and Parallel Old are 标记 - 压缩algorithms. Similarly, the former is single-threaded, while the latter can be seen as a multi-threaded version of the former.

CMS uses the 标记 - 清除algorithm, and are concurrent. In addition to the few operations require STW (Stop the world), it can be garbage collected while the application is running. In the case of concurrent collection failure, JVM uses two other compression type garbage collector once garbage collection. Since the emergence of the G1, CMS has been abandoned in Java 9.

G1 (Garbage First) is a new generation across the garbage collector and old age. In fact, it has disrupted the previously mentioned stack structure, the stack directly into a plurality of regions extremely. Each zone can serve as Eden District, Survivor's area or the old one. It uses 标记 - 压缩algorithms, and are able and CMS as garbage collection concurrently while the application is running.

G1 garbage collection can be performed for each segment region. In selecting regional garbage collection, it will give priority to more deaths recycling target area. This is also the origin of the name G1.

to sum up

This article is about the specific JVM garbage collection methods, from the law of survival of the object, leads to the recovery process, combined with multi-threading features, progressive optimization, and ultimately produced what we now know can be all kinds of garbage collector.

Are interested can visit my blog or follow me number of public, headline number, maybe there will be surprises.

https://death00.github.io/

Guess you like

Origin www.cnblogs.com/death00/p/11706742.html