Deep understanding of Java Virtual Machine reading notes two garbage collector

Source: Internet
Author: User

1. The object is dead?

a, Reference Counting Algorithm: The disadvantage is that it is difficult to solve the problem of mutual circular reference between objects, the Java language does not choose it.

b, root Search algorithm (GC Roots tracing): through a series of objects named "GC Roots" as the starting point, start down search, traversed by the path is called the reference chain, when an object does not have any reference chain connected, the surface of this object is Unreachable. In the Java language, objects that can be used as GC roots Include:

The referenced object in the virtual machine stack (the local variable table in the stack frame).

The object referenced by the class static property in the method Area.

The object referenced by a constant in the method Area.

The referenced object of JNI in the local method stack.

C. references: Java divides references into strong references, soft references, weak references, and virtual References. From Strong to Weak.

Strong Reference: The garbage collector will never reclaim the referenced object as long as the strong application is Present.

Soft reference: before the system is about to have a memory overflow exception, the object is listed in the collection scope and a second recycle. If there is not enough memory after this collection, a memory overflow exception will be thrown. SoftReference

Weak Reference: An object that is associated with a weak reference can only survive until the next garbage collection Occurs. WeakReference

Virtual Reference: whether an object has a virtual reference exists, does not have an impact on its lifetime, and cannot obtain an object instance through a virtual reference. The only purpose is to receive a system notification when an object is Recycled. Phantomreference

d, Survive or die?

After the object is unreachable, enter the "probation" stage. After the first root search, there are two cases of unreachable objects: one is direct recycle, the other is put into F-queue Queue. The condition for putting the F-queue queue is to overwrite the Finalize () method and not be Called. To summarize, the object can save itself once by the GC by overwriting the finalize () Method. Note: the Finalize () method of any object is called only once by the System.

e, Recovery Method Area

The collection of method areas is mainly focused on: obsolete constants and useless classes. Recycling of obsolete constants is similar to the collection of Java heap objects, where garbage collection needs to meet 3 conditions: It can only be recycled and controlled by Parameters.

All instances of this class have been recycled

The ClassLoader that loaded the class have been recycled

The class object that corresponds to it is not referenced anywhere.

Scenarios that use bytecode frameworks such as reflection, dynamic proxies, cglib, and dynamic generation of frequently customized classloader such as JSPs and OSGi require virtual machines to have class offload Capabilities.

2. Garbage collection algorithm

a, mark-clear Algorithm: divided into two stages of marking and Clearing. All objects that need to be reclaimed are marked first, and all tagged objects are collected uniformly after the mark is Complete.

A bit: simple to implement, direct logic

Disadvantage Two: low efficiency, space discontinuity, A large number of fragments.

b, replication algorithm: in order to solve the problem of Efficiency. Divide the memory into two pieces, use only one piece at a time, and copy and replace as soon as you run Out.

Advantages: Simple implementation, Efficient operation

Disadvantage Two: available memory is half the original, and when the object has a high survival rate, replication is Inefficient.

The hotspot virtual machine matches Ibm's research, improves the algorithm, and divides it into a larger Eden area and two smaller survivor Areas. More efficient, but requires guaranteed memory. Compared with the new generation of Use.

c, tag-collation algorithm: do not clean recyclable objects directly, Let all surviving objects move to one end, and then clean out the memory outside the Boundary. More suitable for use in the old Age.

d, Generational Collection algorithm: divides the memory into several blocks according to the Object's survival Cycle. The most appropriate collection algorithm is used according to the characteristics of each age. The new generation generally uses the replication algorithm, the old age uses the mark-sweep algorithm or the marker-collation algorithm.

3. Garbage collector

  

There are 7 collectors that act on different generations, and the lines indicate that the collectors can be used with each other.

a, Serial Collector:

It is a single-threaded collector that uses only one CPU and one collection thread to complete the garbage collection Work. At the same time, it is also a serial collector: when garbage collection is done, all other worker threads (Stop The World) must be paused until it is collected at the End. When the user is not visible, the User's normal work threads are all Stopped.

This simple and efficient collector (especially for a single CPU environment) is suitable for virtual machines running in client Mode.

b, parnew Collector

is a multithreaded version of the serial collector, and the only difference from the serial collector is that multiple threads are used for Collection.

Currently only serial and parnew can work with CMS Collectors. CMS is the first concurrent collector introduced in JDK1.5: the garbage collection thread and the user thread can work at the same time. Parnew has a performance advantage in Multi-cpu Situations.

c, Parallel Scavenge Collector

is a new generation collector, using the replication algorithm, parallel multi-threaded Collector. It is characterized by a focus on achieving a controllable throughput, maximizing the time to use the Cup. At the same time, the Parallel scavenge collector can also turn on the adaptive adjustment strategy via a switch parameter.

d, Serial Old Collector

It is the old age version of the serial collector, single threaded, tag-collation algorithm. Mainly used in the client Mode. At the same time: can be used parallel scavenge collector, as the back plan of the CMS Collector.

e, Parallel Old Collector

It is the old-age version of the parallel scavenge collector, multithreaded and tagged-collation Algorithms. Build a combination of throughput priorities with Paralle Scavenge. Consider using in situations where throughput and CPU sensitivity are a Priority.

f, cms collector (Concurrent Mark Sweep)

In order to obtain the shortest recovery time as the goal, it is suitable for the service-oriented corresponding speed of the Occasion. Based on the Tag-purge algorithm. The entire process is four steps:

Initial tag (CMS initial mark)

Concurrency token (CMS concurrent Mark)

Re-tagging (CMS Remark)

Concurrent Purge (CMS concurrent Sweep)

Note: the initial tag and the Re-tag have the same stop the WORLD. The initial tag just flags the object that the GC roots can directly relate to, the speed is fast, the concurrent tagging phase is the process of roots tracing, and the re-tagging is to fix the tag record of the part of the object that caused the tag to change during the concurrent tag, due to the user Program's Operation. The pause time is not long. Long-time concurrency tokens and concurrent cleanup processes, which collect threads and user threads work Together.

Disadvantages:

Very sensitive to CPU Resources. Default number of startup Threads: (number of CPUs +3)/4.

Unable to process floating garbage, concurrent Mode failure failure may occur resulting in a full GC. The memory reserved during CMS operation could not meet the needs of the program, concurrent Mode failure failed. The virtual machine initiates a backup plan and temporarily uses the serial old collector to collect garbage from the new Age.

The use of the Tag-purge algorithm results in a large amount of memory Fragmentation. May result in an advance full GC. Use parameters to control the frequency of space defragmentation that comes with the full gc.

g, G1 Collector

There are two improvements compared to cms: based on the tag-collation algorithm, the memory fragmentation problem is eliminated and the duration of the pause is precisely controlled. G1 the entire Java heap into separate, fixed-size areas, tracking the extent of area garbage accumulation, maintaining a prioritized list in the background, and prioritizing the most garbage-collected areas per time based on the allowable collection Times.

4. Summary of garbage collector parameters

Deep understanding of Java Virtual Machine reading notes two garbage collector

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.