Java GC (2)

Source: Internet
Author: User

The new generation and the old age are introduced, and then we analyze the concrete composition of the Cenozoic.

The new generation is used to save those newly created objects, which can be divided into three distributions: one Eden Space (Eden), two survivor space (Survivor)

The order of execution for each space is as follows:

    1. Most of the objects that have just been created will be stored in the Eden space.
    2. After the first GC was performed in Eden Space, the surviving objects were moved to one of the survivor spaces.
    3. Thereafter, after the GC is executed in Eden Space, the surviving objects are stacked in the same survivor space.
    4. When one survivor is saturated with space, the surviving object is moved to another survivor's space. This will then empty the survivor space that is already saturated.
    5. In the above steps, repeated several times the surviving objects will be moved to the old age.

The process above is the process of moving data to the old age through frequent minor GC. At the same time, two survivor are used only, and the other one is used to replicate the GC.

In the above process, the hotspot virtual machine uses two techniques to speed up memory allocation. The bump-the-pointer and tlabs(thread-local Allocation buffers) are respectively.

bump-the-pointer Technology tracks the last object created in the Eden space. This object will be placed at the top of the Eden space. If you need to create an object later, you only need to check if the Eden space has enough space left. If there is enough space, the object will be created in the Eden Space and placed at the top. In this way, each time you create a new object, you only need to examine the object that was last created. This will greatly speed up memory allocation. However, if we are in multi-threaded situations, things will be very different. If you want to store objects in the Eden space in a thread-safe manner, you inevitably need to lock them up, which will greatly affect performance.

Tlabs is the hotspot virtual machine solution for this problem. The scheme allocates a single piece of space in the Eden space for each thread, so that each thread accesses only their own tlab space, and the combination with Bump-the-pointer technology can allocate memory without locking.

These two techniques do not have to be deliberately remembered, just have an understanding. It is to be remembered that the newly created object is saved in the Eden space, and those long-lived objects will be transferred to the old age space through survivor space.

Next we analyze the plays in GC, the GC processing mechanism of the old age. The GC events of the old age space are basically when the space is full and the process is different depending on the GC type, so we need to understand the various GC types. There are 5 types of GC in JDK7:

    • Serial GC
    • Parallel GC
    • Parallel Old GC (Parallel compacting GC)
    • Concurrent Mark and Sweep GC (CMS)
    • Garbage first (G1) GC

The first of these serial GC should not be used in the server because this is a serial GC (the earliest GC type that occurs in the single-core CPU ERA), and the Stop the world frequently occurs, thus severely impacting server performance.

Here we analyze the process of each GC type in detail:

1. Serial GC (-XX:+USESERIALGC)

The GC mode of the Cenozoic space we have already introduced, in the old-age space GC method called mark-sweep-compact algorithm.

    1. The first step of the algorithm is to mark the surviving objects in the old age. Tag
    2. The second step is to check the heap memory space from the beginning and leave only the objects that are still surviving. Clean
    3. From the beginning, it fills the heap memory space sequentially and divides the memory space into two parts: one holds the object and the other is empty. Compression

2. Parallel GC (-XX:+USEPARALLELGC)

Differences between Serial GC and Parallel GC

The difference between Serial GC and parallel GC can be clearly seen, Serial GC uses only one thread to perform GC, and parallel GC uses multiple threads, so parallel GC is more efficient. This GC is useful in memory-rich and multicore situations, and is also known as the throughput GC.

3. Parallel old GC (-XX:+USEPARALLELOLDGC)

The Parallel old GC appears after JDK5, and the only difference compared to the Parallel GC is the GC algorithm for the older generation. The Parallel old GC is divided into three steps: Tag-summarize-compress (mark–summary–compaction). The summary (summary) step differs from Cleanup (sweep) in that it distributes the surviving objects to different areas of the GC that are pre-processed, and the algorithm is slightly more complex relative to cleanup.

You can use the parameter-xx:parallelgcthreads=n to specify the number of threads in parallel.

4. CMS GC (-XX:+USECONCMARKSWEEPGC)

Serial the difference between GC and CMS GC

As you can see, the CMS GC is much more complex than the various algorithms I explained earlier. The first step of initializing the tag (initial mark) is simpler. This step simply looks for those surviving objects that are closest to the ClassLoader. Therefore, the time to pause is very short. After the parallel tag ( concurrent Mark ) step, all objects referenced by the surviving object are confirmed to have been traced and verified. This step differs in that the other threads are still executing during the tagging process. At the re-tagging (remark) step, the objects referenced by the surviving object that were added or deleted in the parallel tag step are checked again. Finally, in the parallel exchange ( concurrent sweep ) step, the garbage collection process is forwarded. Garbage collection work is performed during the execution of other threads. Once this GC type is taken, the pause time caused by the GC can be extremely short. The CMS GC is also known as a low latency GC. It is often used in applications where response times are demanding.

Of course, this GC type has the advantage of having a short stop-the-world time, as well as the following drawbacks:

    • It consumes more memory and CPU than other GC types
    • Compression steps are not supported by default

You need to think carefully before using the CMS GC type. If the compression task has to be performed because of too much memory fragmentation, the Stop-the-world takes longer than any other GC type, so you need to consider the frequency and execution time of the compression task before using the CMS.

5. G1 GC

Finally, let's learn the garbage collection first (G1) GC type.

structure of the G1 GC

If you want to understand G1, first you should forget the concepts of the new generation and the old age you have learned. As you can see, each object is assigned to a different lattice, which is then performed by the GC. When an area is filled, the object is assigned to another area, and the GC is executed there are no more than three steps to move from the Cenozoic to the old age. This type is created to replace the CMS GC because the CMS GC has many problems when it continues to function for a long time.

The biggest benefit of G1 is performance, which is faster than any GC we've discussed above. But in JDK 6, he was just an early trial version. Officially released after the JDK7. Personally, NHN requires a long test period (at least one year) before the JDK 7 is formally put into business. So you may need to wait a little longer. And, I've heard a few times that I've been using G1 in JDK 6 to cause a Java virtual machine to crash. Therefore, it is not recommended to use this GC method in a formal project, until it is stable enough.

Not to be continued ...

Java GC (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.