JVM learning (III): garbage collection algorithm

The principle of locality and generational recycling ideas

When university operating system or computer composition principles mentioned an important concept called locality principle.

It refers to the principle of locality CPU accesses the memory , the access instruction or whether it is to access data, the accessed memory cells have tended to gather in a small area of the continuous.

It was discovered that this principle is not just talking about memory cache (Cache), access memory (RAM), disk (ROM) are localized. In fact, the use of redis in the project, such as cache memcache also reflects the importance of the principle of locality.

In the heap space Java virtual machine, active objects tend to be small part of it will be recycled several times after most of the objects are created, which allows the introduction of the Java Virtual Machine generational recovery algorithm. I do not know Sun's researchers are not inspired by the principle of locality, but I do understand the principle of locality is by generational thinking.

Generational recovery is to heap space into the new generation and the old era. The new generation is used to store the newly created object. When an object has survived a long time to move it to the old era. The characteristics of the new generation and the old age, Java virtual machine can be different recovery algorithm.

Most of the new generation of objects survival time is very short, so short recovery time-consuming algorithms can be used, the new generation of trigger GC commonly called Minor GC.

Object old age can often survive for long periods, so the frequency of garbage collection and old age is not high, often run out of time will heap space for garbage collection for the old era. But the recovery of old age is usually full heap scan to find out the space can be recovered, which consumes a lot of time. Modern garbage collector will avoid scanning whole heap by various means. GC old's called the Full GC.

This article, we focus on is Minor GC, that is, the garbage collector for the new generation.

Memory partitioning and recovery mechanisms of the new generation

Previous mentioned replication algorithm, his idea is to heap space is divided into 1: 1 in two parts, and two from and to maintain pointer. Since Java virtual machine active target is a small part, so in fact copy algorithm does not need to maintain 1: 1 ratio (theoretically Sun given by the company is 98% of the objects are recovered with just a few of). According to our forecast assumes that the new generation will be the death of most of the objects, use the copy algorithm requires only a small amount of data replication algorithm the effect will be very good. Thus the new generation has been divided into two zones and Eden Survivor areas the same size.

The default size of Survivor areas are adjusted automatically, also can be adjusted manually. Note that, the more Survivor allocated memory area, the lower the efficiency of the use of heap space.

image.png

When we use the new instruction, new instructions will be carved out as a memory to store an object in the Eden area. If the new Eden area is full of new objects, this time will trigger a Minor GC. Minor GC surviving objects are moved to Survivor areas. Java virtual machine recording Survivor region of the object is replicated many times.

Speaking articles replication algorithm to keep up with, like, to a pointer to an empty area of ​​Survivor. When Minor GC, Eden area and the live objects are copied from the region pointed to, and then switching from and to the pointer. So that the next time there is Minor GC to ensure the content is empty.

What's old promotion opportunity

There are two cases to make the new generation of objects promoted to the old era. The first is an object copy number reaches the set value. If an object is copied 15 times (the default is 15, can be used -XX:+MaxTenuringThresholdto modify), then this object will be promoted to the old era. Survivor second is occupied by more than 50% (available -XX:TargetSurvivorRatiowhen modifying), the virtual copy to copy objects to the higher number of old era.

How Minor GC avoid scanning whole heap

We hope that the new generation of GC Roots scan only when Minor GC, however old the object's possible to refer to the new generation of object. In this case we can not expect, so Minor GC reference must be considered when the old year to the new generation of the object, the new generation of the reference years old to join GC Roots inside. You will find that the new generation of GC to scan the old era, is it not the same with the whole heap scan yet?

Hotspot virtual machine uses a card table (Card Table) technology to solve this problem. Specific operation is the entire stack a Java virtual machine into a 512-byte card, and maintains a table for storing identification bits of each card. The flag on behalf of the card contains a reference to the new generation of object. If you think there is this card it is dirty.

Minor GC so when you can not scan the whole heap, but to find dirty card from the card table and dirty card objects added to the GC Roots in. After the dirty end of the card scanning empty flag.

Write barrier (write barrier)

In the explanation actuators, Java virtual machine needs to intercept the operation of each update may be cited, and the flag corresponding to the position marked dirty. This may point to ensure that each new generation of card objects are marked to.
But in the instant machine code generated by the compiler, the piece of code is not under the Java virtual machine management. So this part of the code you need to insert additional logic, which is called the write barrier.
Write barrier will not determine whether the object points to the new generation, but to believe that this area has pointed to the new generation. This simplifies the instruction into one simple shift operation. Although the write barrier brought a performance cost, but it increased the Minor GC throughput. Therefore these costs is worth it.

Virtual Shared (false sharing)

Suppose CPU cache line size is 64 bytes, since a card entry (one byte), which represents a total of 64 cards. HotSpot each card page is 512 bytes, then a cache line will correspond to a total of 64 cards page 64 * 512 = 32KB.
If the different threads of object reference update operation, just located in the same 32KB area, which will lead to the same cache line simultaneously update the card table, causing write-back cache line, invalidated or synchronous operation, the indirect impact of program performance.
Hotspot introduces new parameters -XX:+UseCondCardMarkto reduce operating write card table. Before performing the write barrier, the first judge to do something simple. If the card has been identified through the pages, no longer be identified. Pseudo-code as follows:

if (CARD_TABLE [this address >> 9] != 0)
  CARD_TABLE [this address >> 9] = 0;

to sum up

To summarize herein, this departure from the principle of locality raises the Java Virtual Machine generational recycling ideas. Generational recovery means, heap memory is divided into the old and the new generation's, and different garbage collection algorithms.

Wherein the new generation is divided into two zones and Eden Survivor areas the same size. The new generation of GC become Minor GC. Minor GC time zone and live objects from Eden pointed Survivor zone will be stored to Survivor zone points. When Survivor areas subject to a certain copy number or value Survivor areas of space usage exceeds a certain value, the object will be promoted to the old era.

Old problems may arise for the object's Minor GC include the new generation of object references, Hotspot virtual machine is to use technology to solve the card table.

Reference article

The JVM card table (Card Table)
Zheng Yu Di: thorough dismantling virtual machine
-depth understanding of the Java Virtual Machine: JVM advanced features and best practices

Guess you like

Origin www.cnblogs.com/rever/p/11433109.html