开发者

Generational Garbage Collection

As I understand, a generational GC divides objects into 开发者_如何学JAVAgenerations.

And on each cycle, GC runs on only one generation.

Why? Why Garbage Collecting of only one generation is enough?

P.S: I understand all these from here .


If you read the link I provided in earlier question you had about Generational GC, you will understand why it does so, the cycle is when the white set memory is filled up.

To optimize for this scenario, memory is managed in generations, or memory pools holding objects of different ages. Garbage collection occurs in each generation when the generation fills up. Objects are allocated in a generation for younger objects or the young generation, and because of infant mortality most objects die there. When the young generation fills up it causes a minor collection. Minor collections can be optimized assuming a high infant mortality rate. The costs of such collections are, to the first order, proportional to the number of live objects being collected. A young generation full of dead objects is collected very quickly. Some surviving objects are moved to a tenured generation. When the tenured generation needs to be collected there is a major collection that is often much slower because it involves all live objects.

Basically, each objects is divided into generations (based on the hypothesis about the object) and places them into a memory heap for a particular generation. When that memory heap is filled up, the GC cycle begins, and those objects that still references are moved to another memory heap and fresh objects are added.


It's not always enough -- it's just that it's usually enough, so it saves time by not examining objects that are likely to stay alive anyway.

Every object has a generation, saying how many garbage collections it has survived. If an object has survived a few garbage collections, chances are that it will also survive the next one.

MSDN has a great explanation:

A generational garbage collector makes the following assumptions:

  • The newer an object is, the shorter its lifetime will be.
  • The older an object is, the longer its lifetime will be.
  • Newer objects tend to have strong relationships to each other and are frequently accessed around the same time.
  • Compacting a portion of the heap is faster than compacting the whole heap.

Because of this, you could save some time by only trying to collect younger objects, and collecting the older generations only if that doesn't free up enough memory.


The answer is there really.

It has been empirically observed that in many programs, the most recently created objects are also those most likely to become unreachable quickly (known as infant mortality or the generational hypothesis).

And

Generational garbage collection is a heuristic approach, and some unreachable objects may not be reclaimed on each cycle. It may therefore occasionally be necessary to perform a full mark and sweep or copying garbage collection to reclaim all available space.

Basically, generational collection gives you better performance over a full garbage collection at the cost of completeness. That's why a mixture of the two is used in practice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜