Garbage collector and memory allocation policy

Source: Internet
Author: User

There is a "high wall" between Java and C + + that is surrounded by dynamic memory allocations and garbage collection techniques, and people outside the walls want to go inside and think out of the walls.

First, judge whether the object is dead

1. Application Counting algorithm

Add a reference counter to each object, and whenever there is a place to reference it, the value of the counter is incremented by 1, minus 1 when the application expires, and any time the counter is 0 indicates that the object is not available.

However, the Java Virtual machine does not choose this algorithm because it is difficult to solve the problem of mutual reference between objects.

  2. Accessibility Analysis algorithm

Through a series of objects called "GC Root" as a starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain), when an object to GC root does not have any reference chain connection, it proves that this object is not available.

  

In the Java language, the object that is the GC root is as follows

① the object referenced in the virtual machine stack.

② the object referenced by the class static property in the method area.

③ the object that is referenced by the constant in the method area.

④ the object that is applied by the JNI (native method) in the local method stack.

Second, garbage collection algorithm

1. Tag-Clear algorithm

"Tag": First mark all objects that need to be recycled.

Clear: The Tagged object is reclaimed uniformly after the tag is complete.

Deficiencies: ① efficiency issues, marking and cleaning two process efficiency is not high. ② tag Cleanup results in a large amount of discontinuous memory fragmentation.

 2. Copying algorithms

Divide the memory into two equal-sized two blocks, use only one block at a time, and when this piece of memory is exhausted, copy the surviving objects to another, then clean up the used memory space once.

Benefits: Recycling is a one-time recovery and does not take into account complex situations such as memory fragmentation. Simply move the pointer over the top of the heap to make it simple and efficient.

Cons: Wasting general memory.

  now the algorithm used by commercial virtual machines recycles The new generation: Divide the memory into a larger Eden space and two smaller survivor spaces, each using Eden and one of the survivor. When recycled, the surviving objects in the Eden and survivor areas are copied one at a time to another survivor space, and finally the Eden area and the survivor area just used are cleared. The hotspot default Eden and Survivor ratio is 8:1, so only 10% space is wasted. When survivor space is insufficient, it is necessary to rely on other memory (old age) for allocation .

  3. Labeling-Sorting algorithm

The replication algorithm will be more efficient when the object survival rate is relatively high, the efficiency will become lower, so the old age is not suitable for this algorithm.

  

 4. Split-band Collection algorithm

The memory is divided into several blocks according to the active period of the object: the Java heap is generally divided into Cenozoic and laosheng generations.

New generation: Replication algorithms are generally used.

Laosheng: General use of "mark-sweep algorithm" or "marker-collation algorithm".

Third, the algorithm implementation of the hotspot

1. Enumerate the root nodes

When object accessibility analysis is performed before garbage collection, the sensitivity to execution time is reflected in GC pauses. Because this analysis must be done in a snapshot that guarantees consistency-all execution during the entire analysis is stopped at a certain point in time. GC pauses are an important cause of performance bottlenecks.

Use Oomap to implement fast node enumeration.

  2. Safety Point (SafePoint)

Not every instruction creates oomap, and we create oomap for specific instructions or program-specific locations, such as: Method invocation, loop jump, exception jump, and so on.

How to run all the threads to the security point: ① preemptive interrupt, when the GC occurs, all threads are first interrupted, and if found not on the security point, the recovery thread calls it to the security point and then stops.

  3. Security Zone

A security zone represents a piece of code that does not cause a reference change.

 

Iv. garbage collector

        

  

Garbage collector and memory allocation policy

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.