layout: post title: “Garbage Collect” description: “” category: “java” tags: [garbage collect] —

Basic ideas

  1. Has a young generation and an old generation
  2. Most initial objects allocated in Eden space
  3. Young generation also has two survivor spaces
  4. Old generation is where long lived objects go to die

Java Memory Modal

Minor Garbage Collection
  1. Objects allocated into Eden space
  2. New objects allocated into Eden space
Major Garbage Collection
  1. Triggered when the tenured(old) space is full
  2. Collects both old and young generations
Copying to old generation

JVM will eventually promote to old generation - after a centain number (15) of garbage collectes - if survivor space is full - if JVM has been told to always create objectes in old space by -XX:+AlwaysTenure

Allocating Objects to Old Space
  1. Objects over a certain size will be allocated directly in old space
  2. Option -XX:PretenureSizeThreshold=<n>
What does live Mean
  1. Live roots

what about reference from Old generation to young generation?

so when this happened, do we need to scan old space when Minor Garbage Collection?

the answer is no, jvm use card table to help

Card table
  1. Each write to a reference to a young object goes through a write barrier
  2. This barrier updates a card table entry
  3. Minor GC scans table looking for the areas that contain references
  4. Load that memory and follow the reference

Different Garbage Collectors

  1. Serial generational collector
  2. Parallel for yount space, serial for old space generational collector
  3. Parallel young and old space generational collector
  4. Concurrent mark sweep with serial young space collector
  5. Concurrent mark sweep with parallel young space collector
  6. G1 garbage collector
Serial Collector
  1. single threaded
  2. Mark and sweep
  3. Ok for small applications running on the client
Parallel Collector
  1. Multiple threads for minor collection
  2. Single thread for major collection
  3. Same process as Serial
  4. Use on Servers
parallel Old Collector
  1. Multiple threads for minor and major collections
  2. Preferred over ParallelGC
Concurrent Mark and Sweep
  1. Only collects old space
  2. No longer bump the pointer allocation
  3. Causes heap fragmentations
  4. Designed to be lower latency

G1
  1. Officially supported in java7
  2. Is a compacting collector
  3. Planned as a replacement for CMS

Tools

  1. MXBeans
  2. Jstat
  3. VisualVM and VisualGC