Algorithms and types of GC

Source: Internet
Author: User

The concept of GC

Garbage Collection Garbage Collection

1960, the list language uses the GC

In Java, GC objects are heap space and permanent zone

GC Algorithm Reference Counting method

Veteran garbage Collection algorithm

Recycle Garbage by reference calculation

The implementation of the reference counter is simple, for an object A, as long as any one object refers to a, then the reference counter of A is incremented by 1, and when the reference is invalidated, the reference counter is reduced by 1. Object A can no longer be used as long as the value of the reference counter for object A is 0.

Issues with reference counters

1. Poor performance

2. Unable to process circular references

Markup Cleanup algorithm

The mark-and-sweep algorithm is the basic idea of modern garbage collection algorithm. The tag-purge algorithm divides garbage collection into two phases: the tagging phase and the purge phase. A feasible implementation is to mark all the objects that can be reached from the root node, first through the root node, in the tagging phase. Therefore, an object that is not marked is a garbage object that is not referenced. Then, in the purge phase, all unmarked objects are cleared.

Tag compression algorithm

Used in situations where there are many surviving objects, such as the old age

It does some optimizations based on the mark-and-sweep algorithm. Like the mark-and-sweep algorithm, the tag-compression algorithm first needs to start with the root node and mark all objects that can be reached. However, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory. After that, clean up all the space outside the boundary.

Copy algorithm (requires a separate space to guarantee)

Compared with the tag-purge algorithm, the replication algorithm is a relatively efficient recovery method
Not suitable for situations where there are many surviving objects, such as the old age
Divide the original memory space into two pieces, one at a time, and at garbage collection, copy the surviving objects in the memory being used into the unused memory block, clear all objects in the memory block in use, swap the two memory roles, complete the garbage collection

Problem:

Wasted Space

Generational thinking

According to the survival period of the object, the short-lived object is classified as the new generation, and the long-life object belongs to the old age

According to the characteristics of different generations, choose the appropriate collection algorithm

Short-lived objects, suitable for replication algorithms

Longevity object for tag cleanup and tag compression

Accessibility

1. Objects that can be touched from the root node

2. The Resurrection

Cannot be touched

1. Untouchable objects cannot be resurrected.

2, can be recycled

3, the Finalize () method may enter a non-reachable state

What is root:

1. Reference objects in the stack

2. Objects that are referenced by static members and constants in the method area

3, the Reference object in the Jin method stack

Stop the word

A global pause phenomenon in Java

Global pause, all Java code stops, Nativa code can execute, but cannot interact with JVM

Mostly due to GC

Damage: Long service stop, no response

Encountering an HA system, which can cause primary and standby switching, seriously compromising the production environment

Algorithms and types of GC

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.