Garbage collection policy in Java

Source: Internet
Author: User

Garbage collection policy in Java
Garbage collection problems

  • Who needs to be recycled
  • When to recycle
  • How to recycle who needs to be recycled

    If an object is no longer used, it can be recycled. The key is how to know that an object is no longer used.

    Reference count

    When an object is referenced, the reference count plus 1. When the reference fails, the Count minus 1. It is simple and intuitive, but the circular reference problem may occur.

    a.tb = bb.ta = a

    Even ifaAndbIt will no longer be used, but the reference count between them is not 0 and cannot be recycled.

    Accessibility Analysis

    The mainstream implementations of Java and C # Use accessibility analysis to determine whether an object is alive. IfGC RootIf an object can be reached, the object is reachable and still stored. Otherwise, the object will not survive and can be recycled.GC RootContains the following types.

    • Virtual Machine stack reference object
    • Object referenced by static property in the Method Area
    • Objects referenced by constants in the method Area
    • When and how to recycle the objects referenced by JNI in the local method Stack

      Let's talk about the specific recycling policies when and how to recycle them.

      Mark collection Algorithm

      The most basic garbage collection algorithm is the first scan, marking all objects that can be recycled, the second scan, and recycling the marked objects.

      Weaknesses

      • Low Efficiency
      • Memory Fragment replication algorithm is generated.

        The replication algorithm divides the memory into two parts, each of which only uses two parts. If the left half is used, the right half is not used. Now that garbage collection is required, copy the left half of the surviving objects to the right half. Then, all the left half is recycled at a time.

        The advantage is that

        • Efficient
        • No memory fragments

          Disadvantage:

          • Low space efficiency, not available for half

            The solution is to adjust the ratio of the two blocks. In the HotSpot, the memory is divided into one Eden and two worker vor. The ratio is8:1Each time an Eden, vor a, and vor B are used for backup. During garbage collection, copy the surviving objects of Eden and vor A to vor B, and then recycle Eden and vor. Next, use Eden, assign vor B, and assign vor.

            The premise of this method is that a large number of objects are dead each time they are recycled, and only a small part is saved. In this way, just copy a small part. However, if a large number of objects survive for a long period of time, it is necessary to copy back and forth repeatedly, which is a waste of life. From this point, we can also see that different recycling policies should be used according to the characteristics of the object survival time.

            Tag Sorting Algorithm

            The tag sorting algorithm is an improvement on the tag clearing algorithm. Mark the object to be recycled during the first scan. Instead of clearing these objects for the second time, they move the surviving objects to the end of the memory area, and they are all connected together. Then, all the places outside the boundary of this area are recycled.

            Generational collection Algorithm

            From the above discussion, we can see that different recycling policies should be used according to the object survival time. Some objects have a short survival time. In this way, there are fewer surviving objects each time they are recycled. You can use the replication algorithm. Some objects have a long survival time. In this way, there are only a few objects to be recycled each time. You can use the tag clearing and tag sorting algorithms. Java heap is divided into the new generation and the old generation. Objects in the new generation have a short survival time and objects in the old generation have a long survival time.

            There are two generational criteria: survival time, object size, and large objects entering the old age.

            HotSpot Garbage Collector

            The HotSpot garbage collector is divided into the new generation recycler and the old generation recycler by generation. The number of Garbage Collector threads can be divided into single-thread recycler and multi-thread recycler. Whether to stop all working threads during garbage collection can be divided into concurrent recyclers and non-concurrent recyclers. Therefore, to see a recycler, you need to understand it:

            • Used for the new generation or the old generation
            • Single thread or multi-thread
            • Whether to stop all worker threads during work
              • By default, the new generation recycler in Client mode adopts the replication algorithm.
              • Single thread
              • All worker threads need to be suspended, ParNew recycler
                • In Server mode, the new generation recycler is preferred, and the replication algorithm is used.
                • Multithreading
                • No need to pause all working threads
                • Only the CMS recycler can work with Parallel Scavenge recycler.
                  • The new generation recycler uses the replication algorithm.
                  • Multithreading
                  • The goal is to achieve a controllable throughput (throughput = running user code time/(running user code time + garbage collection time ))

                    Other recyclers aim to reduce the pause time of user programs due to garbage collection, while Parallel Scavenge recycler aims at the available throughput, because it is also called the throughput priority recycler. There is a conflict here. If the pause time decreases, the user program can get a faster response, but this means that garbage collection becomes frequent, the overall garbage collection time gets longer, and the throughput drops. Available-XX:MaxGCPauseMillisYou can also adjust the pause time of garbage collection.-XX:GCTimeRationAdjust the throughput.

                    Serial Old recycler
                    • The old recycler adopts the mark sorting algorithm.
                    • Single thread
                    • All worker threads need to be suspended
                    • Use in Client Mode
                    • Earlier versions of the Serial recycler Parallel Old collector
                      • The old recycler adopts the mark sorting algorithm.
                      • Multithreading
                      • Parallel Scavenge earlier CMS (Concurrent Mark Sweep) recycler
                        • The old recycler uses the mark clearing algorithm.
                        • Multithreading
                        • In general, you do not need to pause all worker threads.

                          Running process:

                          • Initial tag
                          • Concurrent tag
                          • Remark
                          • Concurrent cleanup

                            Among them, the concurrent mark and re-mark need to suspend all user threads, but the two stages are used less than the other two stages. However, it takes a long time to mark and clear concurrent threads, but they can work with user threads.

                            The initial flag only marks the objects directly associated with the GC Root, and re-marks the objects marked for changes during the modified concurrent flag due to user program running.

                            G1 (Garbage-First) recycler
                            • The new generation and old generation are no longer physically isolated, breaking the original concept of generational Division
                            • Multithreading
                            • No need to pause all working threads
                            • It does not need to be used with other recyclers
                            • Mark the sorting algorithm as a whole, and copy the algorithm locally without generating memory fragments.
                            • Predictable pause, which means that the user can specify how long the recycle operation will be completed.

                              This article is a summary of "deep understanding of Java virtual machines.

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.