Ekaterina
Ekaterina

Reputation: 1892

Do free chunks of memory go to Eden after the compact phase of mark-sweep-compact?

As far as I understand there is is a solid space for young objects and a solid space for old objects in the heap. Objects are moved in both areas during the collections, but in the 2nd area, they are also compacted. My question is why they are compacted, what is the freed solid area used for, Eden or new big old objects?

Upvotes: 1

Views: 61

Answers (1)

Speakjava
Speakjava

Reputation: 3392

Let's look at the HotSpot heap (other implementations can be different).

The Young generation is divided into three regions, Eden space and two survivor spaces. Most objects are allocated initially in Eden space using a simple (very fast) pointer bumping method. To avoid thread contention, each application thread gets its own thread local allocation block (TLAB). When GC happens here, live objects are copied to one of the survivor spaces. Live objects are copied between survivor spaces a number of times (determined by the tenuring threshold) before being copied to the old generation. This is how objects get moved during a minor GC.

Different algorithms handle GC of the old generation (major GC) in different ways. CMS, for example, identifies gaps between live objects and adds them to lists to be used to allocate space for objects being promoted. At some point, fragmentation becomes too great and a full compacting collection occurs. During this, all objects are relocated to be contiguous at the bottom of the heap so there are no gaps.

To answer your question, the old gen is compacted to eliminate fragmentation. The area of memory above the compacted data is then used for objects being promoted during a minor GC. The young and old generations remain separate.

Upvotes: 1

Related Questions