Introduction to the [Java] garbage collection mechanism (garbage Collection)

Source: Internet
Author: User
Tags java se

Automatic garbage collection (Automatic garbage Collection)

Automatic garbage collection is the identification of the heap (heap) memory on which objects are still being used, which objects are not used, and which objects are not being used. So, the garbage here actually means that in memory, there is no longer any object that does not have the value of being present but also occupies the memory space.

The memory allocation and recycle of C language need to be done manually, but in Java, the reclaimed memory is done automatically by the garbage collector.

Garbage collection is divided into two steps: 1. tags, 2. Delete. There are two cases of removing garbage, a. General deletion, B. with compression removed.

1th step. Mark (Marking)

Tags refer to the garbage collector labeling memory blocks, which objects are referenced, and which objects are no longer referenced

The memory block in front of the above tag, followed by the marked memory block.

The referenced memory is blue and is not referenced by Orange. All objects are determined at the time of the tag to be referenced. If all objects in a system need to be scanned, it can be time consuming.

2nd Step, General removal

A regular delete clears the object that is not referenced, retains the object that is still referenced, and points to the free memory space after it is purged.

Is the case after a regular deletion.

The memory allocator holds a reference to free space, allocating memory from free space when new objects are needed.

Step 2a, deleted with compression

To improve performance, you can also compress the space of the remaining referenced objects, in addition to removing objects that are not referenced. By moving the remaining objects, a whole block of memory space is vacated to facilitate later memory allocation.

Is the case after the deletion with compression.

The memory allocator points to the beginning of a whole block of empty memory, which is allocated sequentially when necessary.

JVM generation (JVM Generations)

As mentioned earlier, it is very inefficient to tag and compress all objects in the JVM. As objects are being created more and more, the time for garbage collection is also getting longer. Based on experience, the vast majority of object creation will soon be discarded and have a short lifespan.

Above is an example.

As you can see, the objects that are still being referenced are getting smaller over time. Most objects have a very short life cycle.

Based on the above analysis, the performance of the JVM can be improved by the method of heap generation (generations). Heap memory is divided into three chunks: young Generation, older (old Generation), permanent (Permanent Generation)

The Young generation (Yong Gen), is the place where all objects are created and aged. When the young generation of space is stuffed, it will cause a secondary garbage collection (minor garbage collection). Secondary recovery can be optimized and is an object-based high mortality hypothesis. The young generation that recycles a garbage object is fast. The surviving memory will grow old and will move to the old age space.

older generation (old Gen), used to store objects that have survived for long periods of time. In general, there is a threshold that will be moved to the old age when the age of the younger generation of the object is longer than the threshold value. In the end, the storage of the old age is also recycled, a process known as primary garbage collection (major garbage collection).

Pausing events Worldwide (Stop the World event), both garbage collection and primary garbage collection are events that are suspended worldwide. Pausing events around the world means that all threads of the app are paused until the event is complete.

Typically, the primary collection is slow because it involves all objects. Therefore, for high-response applications, you should try to avoid the main collection events. The JVM provides multiple types of garbage collector. The selection of the garbage collector in the old age will affect the length of time the world's events are suspended due to primary recycling.

The eternal Generation (Permanent Gen)contains the metadata (metadata) used by the JVM to describe the classes and methods in the application. At run time, the JVM populates the content of the immortalized space based on the classes it uses. Classes and methods in the Java SE library may also be stored as in eternity generations. The immortal generation corresponds to class-level memory.

When the JVM discovers that a class is not being used and the space needs to be recycled for other classes, the space for that class may be recycled. Full garbage collection will cover the space of eternity.

Resources

Describing garbage Collection, Java garbage Collection Basics

Introduction to the [Java] garbage collection mechanism (garbage Collection)

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.