Skip to content
Sharp

Memory & GC · interview question

A small object is still referenced for a long time. Put its life in the correct order.

OrderingHard

Put the items in the correct order.

  1. It survives a Gen 1 collection and is promoted to Generation 2.
  2. After it becomes unreachable, a full (Gen 2) collection reclaims it.
  3. It survives a Gen 0 collection and is promoted to Generation 1.
  4. It's allocated in Generation 0.

Answer and explanation

Correct answer

1. It's allocated in Generation 0.
2. It survives a Gen 0 collection and is promoted to Generation 1.
3. It survives a Gen 1 collection and is promoted to Generation 2.
4. After it becomes unreachable, a full (Gen 2) collection reclaims it.

Why

New small objects start in Gen 0. Each collection they survive promotes them one generation, up to Gen 2, which is collected least often. That's why long-lived caches can make full GCs expensive.

Test yourself on Memory & GC

A free test with instant, explained results. No account needed.

Start free test