"Java seconds will play the game. GC" GC algorithm and kind __c language

Source: Internet
Author: User
Tags garbage collection advantage

GC algorithm and category

(i) the concept of GC

GC, refers to the ganbage Collection garbage collector. the GC algorithm mainly divides into four kinds: the reference counting method, the marking elimination, the marking compression, the duplication algorithm. Each of these algorithms is described below.

(II) GC algorithm--Reference counting method

The implementation of the reference counter is simple, for an object A, whenever any object references a, the reference counter of A is added by 1, and when the reference fails, 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.

Question of reference counting method :

(1) References and references are accompanied by addition and subtraction, affecting performance;

(2) It is difficult to deal with circular references;

(II) GC algorithm--mark-Purge algorithm

The mark-purge algorithm is the thought foundation of the modern garbage collection algorithm. The tag-purge algorithm divides garbage collection into two phases:

(1) marking stage : Through the root node, all the accessible objects starting from the root node are marked. As a result, objects that are not tagged are garbage objects that are not referenced.

(2) purge phase : Clears all objects that are not marked.

(III) GC algorithm--tag-compression algorithm

Tag-compression algorithms are suitable for situations where there are more surviving objects , such as the old age . It has done some optimizations on the basis of the tag-purge algorithm. As with the tag-purge algorithm, the tag-compression algorithm first needs to mark all the accessible objects starting with the root node. But after that, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of memory . After that, clear all the space outside the border.

What is the advantage of the "extended" markup compression for tag cleanup?

The advantage is to be able to defragment the memory, avoid allocating large objects, space is not enough to cause FULLGC.

(d) GC algorithm--Replication algorithm

Compared with the mark-purge algorithm, the replication algorithm is a relatively efficient recovery method.

not suitable for the occasion of more survival objects such as the old age;

divide the existing memory space into two pieces, using only one piece at a time, when garbage collection is used, the surviving object in use is copied into the unused memory block, then all objects in the memory block being used are cleared, and the two-memory role is swapped to complete the garbage collection.

The biggest problem with the replication algorithm is: space waste!

(v) Generational thinking

According to the survival cycle of the object, the short-lived object is the Cenozoic, and the long-lived object belongs to the old age.

According to the characteristics of different generations, select the appropriate collection algorithm:

① A small number of objects to survive, such as the new era, suitable for the replication algorithm.

② A large number of objects exist, such as the generation, suitable for mark cleaning or labeling compression

"Note" All algorithms need to be able to identify a spam object, and therefore need to give a definition of accessibility.

(vi) Accessibility

(1) accessible : This object can be touched from the root node.

(2) to be resurrected : Once all references are released, the Resurrection state is possible because the object may be resurrected in Finalize ().

(3) untouchable : After the Finalize (), may enter the untouchable state, the untouchable object can not be resurrected, recyclable.

See the following example:

Package com.liyan.gcTest;
public class Canreliveobj {public
	static canreliveobj obj;
	@Override
	protected void Finalize () throws Throwable {
		super.finalize ();
		System.out.println ("Canreliveobj finalize called");
		obj = this;
	}
	@Override public
	String toString () {return
		"I am Canreliveobj";
	}

	public static void Main (string[] args) throws interruptedexception {
		obj = new Canreliveobj ();
		obj = null; May revive
		System.GC ();
		Thread.Sleep (1000);
		if (obj = = null) {
			System.out.println ("obj is null");
		} else {
			System.out.println ("obj available");
		}
		SYSTEM.OUT.PRINTLN ("second GC");
		obj = null; No Resurrection
		System.GC ();
		Thread.Sleep (1000);
		if (obj = = null) {
			System.out.println ("obj is null");
		} else {
			System.out.println ("obj available")
		;
	}
}

Output results

Canreliveobj finalize called
obj available for
the second time GC
obj is null

"Note" This article mainly summarizes from the network sharing courseware, "smelting number gold--deep JVM kernel" Author: Ge Yi. Only for learning to share.



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.