为什么老年代gc比年轻代的gc久

转自 https://stackoverflow.com/questions/34670751/why-gc-on-old-generation-takes-longer-than-gc-on-young-generation

主要是两点:
- 年轻代的对象通常比较小、存活时间非常短,需要频繁回收 -> 时间优先
- 而老年代的对象相对来说比较大、存活时间长、是慢慢增长的,回收次数不频繁 -> 空间优先

When garbage collection happens memory is divided into generations, i.e. separate pools holding objects of different ages. Almost all most used configurations uses two generations, one for young objects (Young Generation) and one for old objects (Old Generation)

Different algorithms can be used to perform garbage collection in the different generations, each algorithm optimized based on commonly observed characteristics for that particular generation.

Generational garbage collection exploits the following observations, known as the weak generational hypothesis, regarding applications written in several programming languages, including the Java programming language:

• Most allocated objects are not referenced (considered live) for long, that is, they die young.
• Few references from older to younger objects exist.

Young generation collections occur relatively frequently and are efficient and fast because the young generation space is usually small and likely to contain a lot of objects that are no longer referenced.

Objects that survive some number of young generation collections are eventually promoted, or tenured, to the old generation.


This generation is typically larger than the young generation and its occupancy grows more slowly. As a result, old generation collections are infrequent, but take significantly longer to complete.

The garbage collection algorithm chosen for a young generation typically puts a premium on speed, since young generation collections are frequent.

On the other hand, the old generation is typically managed by an algorithm that is more space efficient, because the old generation takes up most of the heap and old generation algorithms have to work well with low garbage densities.

oracle 内存管理白皮书: https://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf

本文来自博客园,作者:mushishi,转载请注明原文链接:https://www.cnblogs.com/mushishi/p/14540922.html

原文地址:https://www.cnblogs.com/mushishi/p/14540922.html