Talking about the memory generation and recovery of Java heap

Source: Internet
Author: User

Overview

Unlike C + +, we don't have to worry about the release of object space in Java, which is largely due to the garbage handlers in Java (GC) that help us automate the release of object space.

Here we take a few questions to learn:

    1. How does heap memory be divided into generations?
    2. How do the different generations work together?
1. How does the heap memory be divided into generations?

Use a picture to describe (the area size does not represent the actual amount of space occupied)

Heap memory is divided into: younger generation (young) + older generation (old), and younger generations:Eden Zone + Survivor District * 2.

Usually the ratio of each region in the younger generation is:eden zone: Survivor0:survivor1 = 8:1:1

To make the object do not enter the old area as much as possible.

S0 and S1 are dynamically automatically resized by default, and you can use '-xx:useadaptivesizepolicy ' to turn off dynamic adjustments

2. How do the different generations work together?

To describe this problem as a copy of an object between each region, we assume that the object has survived.

When we write a code like this,

New User ();

The user object is first placed in the Eden area, where the first minor GC occurs when the Eden is full, and the garbage collector randomly selects a zone in S0 and S1 (assuming S0 is selected) to hold the remaining surviving objects in the Eden area. The second minor GC occurs when the Eden Zone is full again, when the garbage collector copies the surviving objects in the Eden area + S0 to S1, and when the Eden Zone is full again, a third minor GC occurs, and the garbage collector will keep the Eden area alive Objects + The surviving objects in the S1 are copied to the S0, so they are duplicated.

So, under what circumstances will the object enter the old area?

    • When the Eden Zone is full, when an object is about to enter S0 or S1, the object is copied to the old area if S0 or S1 does not hold an object in the Eden area.
    • If an object undergoes a set number of copies in S0 and S1, it is copied to the old area.

By the way: The Young generation garbage collection uses the copy algorithm, the old age recycling uses the mark clears the algorithm.

If the description is not accurate, trouble to specify, thank you!

Talking about the memory generation and recovery of Java heap

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.