Java garbage Collection Mechanism overview

Source: Internet
Author: User

Garbage collection is a large knowledge point in Java, but also a well-known knowledge point, after all, Java claims that its own advanced time will always take garbage collection. So it's also a regular interview, and the interviewer is going to have to explain what the garbage collection is and how it works. Of course, although, and certainly, the interviewer who asked you about garbage collection, he was 99% superficial knowledge. When the interviewer asks you what a garbage collection mechanism is, you should ask the right question: which VM do you ask about the garbage collection mechanism?

Because the concepts involved in garbage collection are complex, the algorithm is complex, and if you want to figure out all the details, it's worth the candle. However, if we master the following summary of the garbage collection mechanism, I believe that in most interviews you will not lose points.

A: What is rubbish?

For example, an object you do not need, it is rubbish, such as:

public void test01 () {
User user = new user ();
//...
}

The test01 method executes, the user object is useless, it is rubbish.

Second: Why garbage collection?

We know that the object is stored on the heap, so how big is the heap? Although the command parameters can be adjusted, typically, under 32-bit systems, the Java heap size is set to 2 GB, with a new generation (Younggen) 1.5 GB allocated to the old age (Oldgen) space. Even 64-bit, think about how much hardware memory our PC can have.

So, useless garbage, all recycled, let out memory space, for other objects.

Third: The JDK default hotspot VM garbage collection mechanism

1: Classification of heap memory

To understand this mechanism, we must first understand the classification of the heap. Yes, we know that the object exists on the heap, but we do not know that the inside of the heap is divided into several spaces, as shown in:

young/new Generation Cenozoic

The interior is also divided into Eden and two survivor Space. New objects are assigned to the Cenozoic,

old/tenured Generation old age

The old age used to store objects that survived after several garbage collections in the program.

( Ps:permanent Generation Non-heap memory for storing static files, such as Java classes, methods, and so on. Persistent generations have no significant impact on garbage collection. )

2: Recycling order

The order of execution for each space is as follows:

      1. Most of the objects that have just been created will be stored in the Eden space.
      2. After the first GC was performed in Eden Space, the surviving objects were moved to one of the survivor spaces.
      3. Thereafter, after the GC is executed in Eden Space, the surviving objects are stacked in the same survivor space.
      4. When one survivor is saturated with space, the surviving object is moved to another survivor's space. This will then empty the survivor space that is already saturated.
      5. In the above steps, repeated several times the surviving objects will be moved to the old age.

Four: Garbage collector and recovery algorithm

Both types of generations have their own collectors, each of which uses a different algorithm. Keep in mind that for beginners, we don't need to master each algorithm principle.

Collectors used by Cenozoic collectors: Serial, pranew, Parallel scavenge

Old age collector used by collectors: Serial, Parallel, CMS

The corresponding algorithm is as follows,

serial collector (copy algorithm)

The new generation of single-threaded collectors, marking and scavenging are single-threaded, with the advantage of being simple and efficient.

Serial Old collector (marker-collation algorithm)

The old age single-threaded collector, the old age version of the serial collector.

parnew Collector (stop-copy algorithm)

The new generation collector, which can be considered a multithreaded version of the serial collector, has a better performance than serial in multi-core CPU environments.

Parallel Scavenge Collector (stop-copy algorithm)

A parallel collector that pursues high throughput and uses CPUs efficiently. Throughput is typically 99%, throughput = user thread time/(user thread time +GC thread time). Suitable for background applications, such as the corresponding requirements for the interaction of the scene is not high.

Parallel Old collector (stop-copy algorithm)

Parallel scavenge collector's old version, parallel collector, throughput priority

CMS (Concurrent Mark Sweep) collector (tag-cleanup algorithm)

High concurrency, low pause, the pursuit of the shortest GC recovery pause time, high CPU consumption, fast response time, short pause time, multi-core CPU to pursue high response time selection

Five: When will the garbage collection run?

There are two types of garbage collection, scavenge GC and full GC.

The scavenge GC is triggered when a new object is generated and the Eden application space fails. This will be garbage collected for the new generation.

When the old generation (tenured) is written full, the persistent generation (Perm) is full, System.GC () is displayed, the domain allocation policy of the heap after the last GC is dynamically changed, and the fully GC is executed.

Note that, regardless of the kind of recycling, does not mean that it will be all the garbage collected, but according to the algorithm's own judgment, in a period of time to remove a certain amount of rubbish, this time and quantity we are not known.

These are the garbage collection mechanisms that you must know.

Java garbage Collection Mechanism overview

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.