layout: post
title: “Garbage Collect”
description: “”
category: “java”
tags: [garbage collect]
—
Basic ideas
- Has a
young generation
and an old generation
- Most initial objects allocated in
Eden space
- Young generation also has two survivor spaces
- Objects that survive a GC get moved to the survivor space
- Only one survivor space in use at a time
- Objects copied between survivor spaces
- Old generation is where long lived objects go to die
Java Memory Modal
Minor Garbage Collection
- Objects allocated into Eden space
- When GC runs objects are copied to newer survivor space
- Objects from older survivor space also copied to newer survivor space
- Survivor spaces are swapped
- New objects allocated into Eden space
Major Garbage Collection
- Triggered when the tenured(old) space is full
- 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
- Objects over a certain size will be allocated directly in old space
- No JVM option to force objects to old space
- Option
-XX:PretenureSizeThreshold=<n>
- all objects larger then bytes should be allocated directly in old space
- however if object size fits TLAB (Thread Local Allocation Buffers), JVM will allocate it in TLAB
- You should also limit TLAB size
What does live Mean
- Live roots
- From stack frames
- static variables
- Others such as JNI (Java Native interface) and synchronization monitors
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
- Each write to a reference to a young object goes through a write barrier
- This barrier updates a card table entry
- Minor GC scans table looking for the areas that contain references
- Load that memory and follow the reference
Different Garbage Collectors
- Serial generational collector
- Parallel for yount space, serial for old space generational collector
- Parallel young and old space generational collector
- Concurrent mark sweep with serial young space collector
- -XX:+UseConcMarkSweepGC
- -XX:-UseParNewGC
- Concurrent mark sweep with parallel young space collector
- -XX:+UseConcMarkSweepGC
- -XX:+UseParNewGC
- G1 garbage collector
Serial Collector
- single threaded
- Mark and sweep
- Ok for small applications running on the client
Parallel Collector
- Multiple threads for minor collection
- Single thread for major collection
- Same process as Serial
- Use on Servers
parallel Old Collector
- Multiple threads for minor and major collections
- Preferred over ParallelGC
Concurrent Mark and Sweep
- Only collects old space
- No longer bump the pointer allocation
- Causes heap fragmentations
- Designed to be lower latency
G1
- Officially supported in java7
- Is a compacting collector
- Planned as a replacement for CMS
- MXBeans
- Jstat
- VisualVM and VisualGC