1. Heap
Heap 分为young generation, old generation.
- young generation, 采用清除算法,分为from, to两个区域。当需要GC时,会检测分配的对象的有效性,如果从这次GC中存活,会标记,然后copy到to 区域,清空from区,然后交换from, to的命名。当有对象在2次GC都存活了,那么会移动到old generation.这个也被称为minor GC.
- old generation, 采用mark-sweep方法, old generation中allocate分配容易,清理难。这个也被称为major GC。这里的GC也有两种可能,一种是内存充裕时使用mark-sweep, 另一种是内存不充足时内存压塑,移动内存,释放内存碎片。
Usually, ~20% of the Young Generation survives into the Old Generation. Collection in the Old Space will only commence once it is getting exhausted. To do so the V8 engine uses two different collection algorithms:
- Scavenge collection, which is fast and runs on the Young Generation,
- Mark-Sweep collection, which is slower and runs on the Old Generation.
node GC时会stop the world,代表主线程被暂停了。