Understanding Java GC (iii)

Source: Internet
Author: User
Tags compact

This article focuses on the following 5 types of GC:

    1. Serial GC (-XX:+USESERIALGC)
    2. Parallel GC (-XX:+USEPARALLELGC)
    3. Parallel old GC (-XX:+USEPARALLELOLDGC)
    4. CMS GC (-XX:+USECONCMARKSWEEPGC)
    5. G1 GC
Serial GC (-XX:+USESERIALGC)

In the previous article, we have explained the GC algorithm used by GC in young generation. The algorithm used by GC in old generation is "mark-sweep-compact".

1. First mark the surviving object in "old generation". (Mark)
2, immediately after the start of the heap check, no use of the object is cleared away, only the remaining in the 1, the tag of the surviving object. (sweep)
3. Finally, all surviving objects are continuously dropped to one end of the heap. At this point, the heap (more accurately, "old generation") is divided into two parts: one with the object and the other for the free space. (compact)

This type of GC is used in cases where memory is small and CPU cores are low.

Parallel GC (-XX:+USEPARALLELGC)

From the figure below, we can clearly distinguish between the serial GC and the parallel GC. That is, the serial GC executes with only one thread, and the parallel GC is executed with multiple threads, so it is faster.
This type of GC is useful when the memory is sufficient and the number of CPU cores is OK. It is also called "Throughput GC." (That is, it can improve the throughput of the system)

Parallel old GC (-XX:+USEPARALLELOLDGC)

The Parallel old GC appears after JDK5. By comparing them with the above parallel GC, we will find that the difference is only in dealing with "old generation".

Specifically, it is divided into 3 steps: mark-summary-compaction In summary This step, from the area marked by the previous step (mark) to identify the surviving objects, this and "mark-sweep -compac "Sweep" is different, it contains a slightly more complex number of steps.

CMS GC (-XX:+USECONCMARKSWEEPGC)


As shown, the CMS GC (Concurrent mark-sweep GC) is more complex than any of the previously mentioned GC types.

The initial initialization tag is a very simple step. The most recent surviving object from the classloader is searched. Therefore, the pause time is extremely short.
In the parallel tag step, the object referenced by the surviving object is confirmed in the previous step and will be tracked and checked. The difference between these two steps is the previous step "Stop-the-world", and this step is performed in parallel with the user thread. In the parallel tag step, the object that has been referenced has just been added or stopped and will be checked.
Finally, in the parallel cleanup step, the GC starts to recycle garbage. GC threads are also executed in parallel with other user threads. Since the CMS GC is executed in this way, it is not surprising that it is temporarily paused for a short time. Therefore, the CMS GC, also known as the "Low Latency GC" (Short wait GC), is often used in environments where response times are extremely demanding.

After saying the benefits of the CMS GC, let's talk about its drawbacks:

    • It consumes more memory and CPU time than other types of GC;
    • The default is not to provide compression steps.

If you still have questions, you can go back and compare the GC types mentioned earlier. Compression can reduce memory fragmentation, but it also takes a long time, especially in "Stop-the-world". You can often check the time-consuming compression work.

G1 GC

To understand the G1 GC (Garbage first GC), you should discard the knowledge you have about young generation and the old generation you now know.

As shown, an object is assigned to a grid and then the GC is executed. Once a space is exhausted, another space is used and the GC begins to clean up. Previously mentioned data in young generation after 3 space transfer will eventually be dropped to the old generation, this cleaning method in the G1 GC is not visible.
G1 GC is used to replace CMS GC, in the long run, the CMS GC will bring a bunch of problems.

The performance of G1 GC is praised. It's faster than any type of GC we're talking about today. But in JDK6, it is considered an early way and can only be used for testing. In the JDK7, however, it has been adopted. I myself feel that we need a lot of testing before NHN uses JDK7 in practical applications. So you may have to wait a while. Of course, I heard that the JVM used the G1 crash several times in the JDK6. So wait for its stable version. (The author raises: JDK 8 is now out, the author of this article was written in 12, do not care ha)

In the following explanation, we talk about GC tuning. Consider this question first:

If the size and type of all objects created by the application are determined and identifiable, then all GC options in the company can be the same. However, the size and life cycle of an object is dependent on the application, and the type is subject to change. In other words, an application that uses the "a" scheme in the GC option does not mean that it can be copied directly to another application. Therefore, through continuous monitoring and adjustment, we can find the most suitable solution for their own application.

Well, next article we'll start by explaining how to monitor and tune the GC.

Understanding Java GC (iii)

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.