When was the JVM garbage collection triggered? Garbage collection algorithm? What are the garbage collector __ Algorithms

Source: Internet
Author: User
Tags garbage collection
1.1. So what does GC do for us?

1, which memory needs to be recycled.

2, when to recycle.

3, how to recycle.

At this point someone will be puzzled, since GC has solved this contradiction for us, we still need to learn GC. Sure, of course, when we need to use it.

1, Troubleshoot memory overflow

2, troubleshoot memory leaks

3, performance tuning, troubleshooting concurrency bottlenecks

1.1.1. How the JVM determines that objects can be recycled.

We know that the GC is primarily dealing with the collection of objects, so when does it trigger a collection of objects?

1, the object is not referenced

2, the scope has not caught an exception

3, the program in the scope of the normal execution completed

4, the program executes the System.exit ()

5. Accidental termination of procedure (killing process, etc.)

In fact, the easiest thing to think about is that when an object is not referenced, it will mark the object as recyclable, so now there is a question whether the object will be marked as recyclable after it has been assigned empty.

It is not that the object is assigned to be empty, it must be marked as recyclable, and it is possible that escape will occur.

1.2. Now let's take a look at some of the garbage collection algorithms 1.2.1. Before JDK1.2, the reference counter algorithm is used,

That is, when this class is loaded into memory, a series of information such as method area, stack, program counter, etc. is generated, when objects are created, objects are allocated in the stack space for this object, a reference counter is generated, reference counter 1 is referenced, and when a new reference is made, the reference counter continues 1, And when one of the references is destroyed, the reference counter-1, when the reference counter is reduced to zero, marks the object has no reference, can be recycled. This algorithm was widely used before the JDK1.2, but as the business developed, a problem soon arose

When our code shows the following scenario, the algorithm will not adapt

a) Obja.obj = OBJB

b) Objb.obj-obja

Such code produces the following reference case Obja points to objb, and objb points to obja, so that when all other references are gone, Obja and OBJB have a reference to each other, that is, the two-object reference counter is 1, In fact, both of these objects have no additional references and are already rubbish.


1.2.2. Root Search Algorithm

The root search algorithm is introduced from the graph theory in the discrete mathematics, the program regards all the reference relations as a graph, starts from a node GC ROOT, looks for the corresponding reference node, finds this node, continues to look for the reference node of the node, and when all the reference nodes are searched, The remaining nodes are considered to be nodes that are not referenced, that is, useless nodes.


Currently, the objects in Java that can be used as GC Root are

1, the object referenced in the virtual machine stack (local variable table)

2, the object referenced by the static property in the method area

3, the object referenced by the constant in the method area

4, the object referenced in the local method stack (Native object)

1.2.3. Classification of references

1, strong references

As long as the reference exists, the garbage collector will never reclaim

Objectobj = new Object ();

The corresponding object such as Obj.equels (NewObject ()) can be obtained directly through the objective lens.

And so the Obj object is a strong reference to the subsequent newobject, and only if the obj reference is released, the object will be released, which is also the encoding form we often use.

2, soft reference

Must be referenced and reclaimed before memory overflows, and can be implemented by using the following code

Objectobj = new Object ();

SOFTREFERENCE<OBJECT>SF = new softreference<object> (obj);

obj =null;

Sf.get ();/sometimes returns null

At this point sf is a soft reference to obj, which can be fetched by the Sf.get () method, which, of course, returns NULL when the object is marked as an object that needs to be reclaimed;
Soft reference to the main user to achieve similar caching functions, in sufficient memory, directly through the soft reference value, without the busy real source query data, improve speed; when there is not enough memory, automatically delete this part of the cached data, from the real source query the data.

3, weak reference

The second garbage collection is reclaimed, which can be done by using the following code

Objectobj = new Object ();

WEAKREFERENCE<OBJECT>WF = new weakreference<object> (obj);

obj =null;

Wf.get ();/sometimes returns null

Wf.isenqueued ()//Returns whether the garbage collector is marked as the garbage that is about to be recycled

A weak reference is reclaimed on a second garbage collection, and the corresponding data is fetched in a short time by a weak reference, and Null is returned when a second garbage collection is performed.

Weak references are used primarily to monitor whether the object has been flagged by the garbage collector as the garbage that is about to be recycled, and can return the object to the garbage collector through the weakly referenced isenqueued method

4. Virtual Reference (Phantom/Phantom Reference)

Garbage collection is reclaimed, the object value cannot be fetched by reference, and can be implemented by using the following code

Objectobj = new Object ();

PHANTOMREFERENCE<OBJECT>PF = new phantomreference<object> (obj);

Obj=null;

Pf.get ();//return NULL Forever

Pf.isenqueued ()//return from memory has been deleted

A virtual reference is reclaimed every time garbage collection is made, and the data that is always fetched by the Get method of the virtual reference is null and is therefore also a phantom reference.

A virtual reference is primarily used to detect whether an object has been removed from memory. 1.3. The method area will also be recycled

。 However, the recovery conditions for the method area are very harsh and are recycled only if the following three conditions are met.

1. All instances are recycled

2, loading the class of ClassLoader is recycled

3, class object can not be accessed by any means (including reflection)

Okay, now we're going to cut to the chase, Java1.2 before the main by reference counters to mark whether the need for garbage collection, and 1.2 after the use of root search algorithm to collect garbage, and the collection of garbage by what algorithm to recycle it.

1, mark-elimination algorithm

2. Copy algorithm

The replication algorithm is scanned from the root set, and the surviving object is copied to a new, unused space, this algorithm when the control of the object is relatively young, extremely efficient, but the cost is to need a memory swap space for object movement. Which is what we mentioned earlier.

S0 S1 and other space.

3, marking-finishing algorithm

Let's take a moment.

1, mark-elimination algorithm

The tag-purge algorithm uses a scan from the root collection, marks the surviving object objects, and then scans the unmarked objects in the entire space for recycling, as shown above.

Tag-purge algorithms do not require the movement of objects and are processed only for objects that are not alive, and are extremely efficient in the case of many surviving objects, but because the tag-purge algorithm directly reclaims the objects that are not alive, it can cause memory fragmentation.

2. Copy algorithm


The replication algorithm is scanned from the root set, and the surviving object is copied to a new, unused space, this algorithm when the control of the object is relatively young, extremely efficient, but the cost is to need a memory swap space for object movement. Which is what we mentioned earlier.

S0 S1 and other space.

3, marking-finishing algorithm


Mark

-The collation algorithm uses the tag-erase algorithm to mark the object in the same way, but when cleared, it moves all the surviving objects to the left free space and updates the corresponding pointers when the unused objects are reclaimed. The tag-collation algorithm is based on the tag-purge algorithm, and the object is moved, so the cost is higher, but it solves the problem of memory fragmentation.

We know that the JVM in order to optimize the recovery of memory, the method of generational recovery, for the generation of memory recovery (minor GC) mainly using the replication algorithm, the following figure shows the minor GC implementation process. 1.4. Let's introduce each of the garbage collector.    1.4.1. 1. Serial Collector

We can see the name. This belongs to the serial collector. Its operation schematic diagram is as follows


Serial

The collector is one of the oldest collectors in the history of JDK1.3, and is currently the default garbage collector for CLIENTVM under the SERVERVM 4 core 4GB below. The serial collector is not only a single CPU to collect, but when the JVM needs garbage collection, it needs to interrupt all the user threads, know that it is finished, and therefore known as the "Stop the World" garbage collector. Note that the JVM Chinese name is Java Virtual machine, so it works like a virtual computer, and each one of these threads is considered a processor of the JVM, so you can see that the CPU0, CPU1 actually the user's thread, not the CPU of the real machine, don't misunderstand.

Serial recovery method suitable for low-end machines, is the client mode of the default collector, CPU and memory consumption is not high, suitable for user interaction is relatively small, background tasks more system.

The serial collector defaults to the new and old generation of the collector with the Serial+serialold 1.4.2. 2. Parnew Collector

The Parnew collector is actually a multi-threaded version of the serial collector, which runs the following schematic

also have

Stop The world problem, he is the preferred collector in multi-CPU mode (The collector is much less efficient than the serial collector in a single CPU environment, so be sure to watch the scene) and the default collector in server mode.

1.4.3.3, Parallelscavenge

Parallelscavenge is also known as the throughput-first collector, which runs the schematic diagram below


Parallelscavenge

The throughput mentioned is = program run time/(time of the JVM performing the Recycle + program run time), assuming that the program has been running for 100 minutes, and the JVM's garbage collection occupies 1 minutes, then throughput is 99%. In today's network to tell advanced, good response speed is to enhance the user experience of an important indicator, multi-core parallel cloud computing development requires the program as much as possible use of CPU and memory resources, as soon as possible to calculate the final results, so in the interaction of the cloud, more suitable for the use of the collector.

1.4.4.4, Parallelold

Parallelold is one of the contemporaries ' parallel collectors, which uses the tagging algorithm and is a collector with priority to the throughput of the senior generation. This collector is a collector that has just been introduced after JDK1.6, we look at the correlation between the previous diagram and see that before the early parallelold, throughput-first collectors can only use the serial collector, which is a big drag on throughput-first performance, since JDK1.6 can really be more efficient Rate of throughput is preferred. Its operation schematic diagram is as follows


1.4.5.5, Serialold

Serialold is the default collector in the old generation client mode, single-threaded execution, and before JDK1.6 is the default collector for the old generation in the Parallelscvenge recovery Cenozoic mode, and also the standby collector after the failure of the concurrent collector CMS recovery. Its operation schematic diagram is as follows



1.4.6.6, CMS

CMS also known as response time priority (Shortest recovery pause) of the collector, use concurrent mode to recycle garbage, use tag-purge algorithm, CMS is very sensitive to CPU, its recycle thread number = (cpu+3)/4, so when the CPU is 2 core benefits, the recycle thread will occupy 50% of CPU resources, When the CPU core is 4, it occupies only 25%. The sketch of his operation is as follows


Cms

The pattern is divided into 4 main processes


At the time of the initial tag, all user threads need to be interrupted, in the concurrent markup phase, the user thread and the tagged thread

Concurrent execution, and in this process, as the memory reference relationship changes, the original tagged object may be released, causing new garbage, which may result in a series of floating garbage that cannot be recycled.

CMS to ensure that all objects are scanned to avoid objects that are not identified in the Initialmarking, the method used is to locate the tagged objects and put them into the stack, scanning for objects that are dependent on the object, if the dependent object's address is before it, The object is marked and placed in the stack, and only if the dependent object address is behind it.

Minor GC may also occur at the same time during concurrentmarking, this time it is easy to cause the old generation object reference relationship change, CMS in order to deal with this concurrency phenomenon, provides a mod uniontable to record, in this mod Union Records in table

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.