Java garbage collection and garbage collection

Source: Internet
Author: User

Java garbage collection and garbage collection

Java collection Mechanism garbage collection gc. This is handled by JVM.

There are two steps for recovery:
1. If you confirm that an object is "junk "?

1) reference counting method if an object is created but not used anywhere, it becomes a recyclable object. Test test = new Test ();

This method is simple and efficient, but it cannot solve the problem of circular reference. Therefore, java does not use this method. python uses reference notation.

2) What is circular reference?
MyObject object1 = new MyObject ();
MyObject object2 = new MyObject ();
// Two objects reference each other
Object1.object = object2;
Object2.object = object1;

// The objects to which both objects are set as null object1 and object2 are no longer accessible because they reference each other and their reference count is not 0, then the Garbage Collector will never recycle them.
Object1 = null;
Object2 = null;

3) So java uses the Accessibility Analysis Method: search by using the "GC Roots" object as the starting point. If there is no reachable path between "GC Roots" and an object, this object is not reachable, and will not be recycled immediately at this time, but will be marked twice at least.
Then, the object will change from an inaccessible object to a recyclable object.

In java, how do I determine common objects as recyclable objects:

1) explicitly assign a reference to null or point the reference that has already pointed to an object to a new object.

Object obj = new Object ();
// The value is null.
Obj = null;

Object obj1 = new Object ();
Object obj2 = new Object ();
// Point to the new object, so the original object will be lost because it points to the new object.
Obj1 = obj2;


2) Locally referenced objects:
For (int I = 0; I <10; I ++ ){
Object obj = new Object ();
System. out. println (obj. getClass ());
}
After an object is executed in every loop, it becomes a recyclable object.


3) weak references to the objects associated with them,
WeakReference <String> wr = new WeakReference <String> (new String ("world "));

 

2. How to recycle


1) typical garbage collection algorithms

After confirming the garbage collection, how can we efficiently recycle the garbage? The following methods

1. Mark-Sweep (Mark-clear) Algorithm

The tag-clearing algorithm is divided into two phases: the tag phase and the clear phase.
Marking stage: Mark the objects to be recycled, Accessibility Analysis
Clear stage: reclaim the space occupied by the marked object

This method is prone to memory fragments. Too many fragments may cause a new garbage collection action in advance when the memory is insufficient for large objects in the subsequent process.

2. Copying Algorithm

Divide the memory capacity into two equal parts and use only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space, in this way, memory fragmentation is not prone.

However, this method will halved the memory control,

3. Mark-Compact Algorithm

The algorithm marking phase is the same as Mark-Sweep, but after marking is completed, it does not directly clear recyclable objects, but moves all surviving objects to one end, and then clears the memory outside the end boundary.

4 Generational Collection (Generational Collection) Algorithm

The generational collection algorithm is currently used by most JVM garbage collectors.

Memory is divided into several different regions based on the lifecycle of the object, which is generally the old age and the new generation.

In the old age: Only a few objects need to be recycled during garbage collection. Generally, Mark-Compact is used.

New Generation: a large number of objects need to be recycled during each garbage collection, so the most appropriate collection algorithm can be adopted according to the characteristics of different generations.
The copying algorithm is used because the majority of garbage collection needs to be collected in each new generation, but it does not divide the space of the New Generation according to the ratio, generally, the new generation is divided into a large Eden space and two smaller volume vor controls.
When using Eden space and a dedicated vor space, copy the surviving objects in the Eden and dedicated vor space to another dedicated vor space, and then clear the Eden and the used dedicated vor space.


2) typical Garbage Collector

1. Serial/Serial Old: it is a single-thread collector and must suspend all user threads for garbage collection.
The Serial collector is designed for the new generation of collectors and adopts the Copying algorithm. The Serial Old collector is designed for the collectors of the Old generation and uses the Mark-Compact algorithm. Its advantage is its simplicity and efficiency, but its disadvantage is that it will bring a pause to users.

2. ParNew is a multi-threaded version of the Serial collector. Multiple Threads are used for garbage collection.

3. The Parallel Scavenge collector is a new generation of multi-thread collector (Parallel collector)
It does not need to suspend other user threads during the collection process. It adopts the Copying algorithm, which is different from the first two collectors, mainly to achieve a controllable throughput.

4. Parallel Old is the Old version of the Parallel Scavenge collector (Parallel collector), using multithreading and the Mark-Compact algorithm. Is it old?

5. The CMS (Current Mark Sweep) collector is a collector designed to obtain the minimum recovery pause time. It is a concurrent collector that uses the Mark-Sweep algorithm.

6. The G1 collector is the most cutting-edge result in the development of collector technology today. It is a collector for server applications and can fully utilize multi-CPU and multi-core environments. Therefore, it is a parallel and concurrent collector, and it can establish a predictable pause time model.

Memory Allocation:
The memory of an object is mainly allocated to the new generation of Eden and From Space. In a few cases, the old generation will arrive. If the new generation of Eden Space and From Space have insufficient Space, a GC will be initiated.

During GC, 1. The surviving objects in Eden Space and From Space are moved To Space, and From and To are two same vor blocks.
2. Then, clear the Eden Space and From Space.
3. If the To Space Control cannot store an object, the object will be moved To the old age.

After gc, Eden space and To Space are used (because the From Space is cleared and the surviving objects are placed in To Space)

The next GC will copy the surviving object to the From Space, which repeats repeatedly. If an object evades GC once in the same vor area, the object age is increased by 1. By default, if the object age reaches 15 years old, it will be moved to the old age.


In general, large objects will be directly allocated to the old age. The so-called large objects refer to objects that require a large number of consecutive buckets. The most common large object is a large array.


Byte [] data = new byte [4*1024*1024]

In general, the storage space is directly allocated in the old age.

Of course, the allocation rules are not fixed, which depends on the current Garbage Collector combination and JVM-related parameters.

 

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.