Chapter 6 JVM garbage collector (2) jvm garbage collection

Source: Internet
Author: User

Chapter 6 JVM garbage collector (2) jvm garbage collection

The previous chapter records several common garbage collectors. For details, see chapter 5 JVM garbage collector (1).

 

1. G1

Note:

  • From the Perspective, compared with CMS, G1 is different only in the final "filter and recycle" section (CMS is a concurrent cleanup). In fact, the overall heap memory division of the G1 recycler is different from that of other collectors.
  • CMS must be used with ParNew and G1 to separate the entire space

Principle:

  • The G1 collector divides the entire heap into multiple Region with the same size.
  • G1 tracks the value of garbage accumulation in each region (the space size obtained after recycling and the experience values required for recycling), maintains a priority list in the background, each time based on the permitted collection time, refresh the most valuable region: scan some of the most valuable region (instead of scanning the entire heap memory) at the specified time, and recycle it, achieve the highest possible collection efficiency within a limited time.

Operation Process:

  • Initial Tag: Mark all referenced objects directly associated with the root node. STW required
  • Concurrent flag: traverse the associated nodes marked previously and continue to mark down all surviving nodes.
    • During this period, all objects that change the reference relationship will be recorded in the Remember Set Logs.
  • Final MARK: the garbage generated during the concurrent mark. STW required
  • Filter and recycle: recycles objects with high value based on the expected time specified by the user (see "Principle" article 2 ). STW required

Advantages:

  • The pause time can be predicted: we specify the time, within the specified time only to reclaim some of the most valuable space, and the CMS needs to scan the entire old generation, and the pause time cannot be predicted.
  • No memory fragments: space is integrated after garbage collection. CMS adopts the "mark-clean" algorithm, and memory fragments exist.
  • Screening and recycling stage:
    • Because only part of the region is recycled, we can control the STW time, so we do not need to compete for CPU resources with the user thread concurrency, and CMS concurrent cleaning requires a portion of the CPU, which will reduce the throughput.
    • Because STW does not generate "floating garbage" (that is, the garbage that cannot be recycled by CMS in the concurrent cleaning stage)

Applicability:

  • Short pursuit of STW: If ParNew/CMS is used well, use this; if not, use G1
  • Throughput pursuit: Parallel Scavenge/Parallel Old, while G1 has no advantage in Throughput

 

2. Notes

Question 1Other collectors except G1 are not only scanning the young generation, but also the old generation. During the recovery process of the young generation, if the objects in the old generation reference the objects of the young generation, we will not be able to scan the young generation, however, since the old generation is generally three times the size of the young generation, if the young and old generations Scan together, the efficiency will drop sharply. How can this problem be solved?

Answer:: JVM uses remember set to do this. When a referenced object is assigned a value, JVM sends a write barrier command to temporarily interrupt write operations, check whether the referenced object is in the old generation, and whether the referenced object is in the new generation (that is, whether the old generation object references the young generation object). If yes, record the reference information to the remember set. After scanning, We will scan down from the root set + remember set. (That is, the real root set is the root set defined by JVM + remember set)

 

Question 2To make GC time predictable, the G1 collector scans the region with the largest value. If the objects in this region are referenced by other region objects, so scanning only the former may not work, but if scanning all region, GC time cannot be predicted, and efficiency will be greatly reduced. What should I do?

Answer:: G1 similarly, a remember set is assigned to each region. When a referenced object is assigned a value, JVM sends a write barrier command to temporarily interrupt write operations, check whether the referenced object is in a different region (eg. a = B; check whether a and B are in different region). If yes, record the reference information to the remember set of the current region. After scanning, We will scan down from the root set + remember set.

 

Question 3When CMS and G1 are concurrently marked, if the reference relationship of some referenced objects has changed, how can this problem be solved so that only these changes can be scanned during re-marking?

Answer:: During the concurrent tag period, if the object's reference relationship changes, these related records will be recorded in the remember set logs; During the remarking stage, add the logs information to the remember set, and then trace the node from the remember set.

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.