How does garbage collection work and how is it optimized?

Source: Internet
Author: User

1) Garbage collection is a process that reclaims useless memory space and makes it available to future instances.

Eden Zone : When an instance is created, it is first stored in the heap memory of the young generation's Eden area.

Note : If you don't understand these words, I suggest you read this garbage collection introduction, which describes the memory model, the JVM architecture, and these terms in detail.

Survivor zones (S0 and S1): As part of the young generation GC (Minor GC) cycle, the surviving objects (still referenced) are moved from the Eden area to S0 in the Survivor area.

Similarly, the garbage collector scans the S0 and then moves the surviving instance to S1.

S0: Should this not be the case for Eden and the survival of the S1, why move to S0 and then move from S0 to S1? )

Instances of death (no longer referenced) are marked as garbage collection. Depending on the garbage collector (four commonly used garbage collector, which will be described in the next tutorial), either the flagged instance will be removed from memory continuously, or the recycle process will be completed in a separate process.

Old Age: the old or tenured generation is the second logical area in heap memory. When the garbage collector performs the Minor GC cycle, the surviving instances in the S1 Survivor Zone are promoted to the old age, and unreferenced objects are marked for recycling.

old age GC (Major GC): The old age is the last phase of the instance life cycle relative to the Java garbage collection process. Major GC scans the garbage collection process in the old years. If instances are no longer referenced, they will be marked as recycled or they will remain in the old age.

Memory Fragmentation : Once an instance is removed from the heap memory, its location becomes empty and can be used for future instance allocations. These vacated spaces will fragment the entire area of memory. For quick allocation of instances, defragmentation is required. Based on the different choices of the garbage collector, the reclaimed memory area is either kept organized or completed in a separate GC process.

2)

Reduce the number of objects transferred to the old age to a minimum;

2 , reduce Full GC time of execution;

In order to achieve the above purpose, generally, the things you need to do are:

1, reduce the use of global variables and large objects;

2, adjust the size of the new generation to the most appropriate;

3, set the size of the old age is the most suitable;

4. Select the appropriate GC collector

How does garbage collection work and how is it optimized?

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.