GC Flashcards
Why we have GC?
1) Auto memory management(allocate/free) - less memory leak
2) Executed by a Daemon thread ‘Gabbage Collector’
GC supposed to work on 2 hypothesis?
1) Most obj will soon become unreachable - Most objects are exists in a method scope
2) ref from ‘old’ obj to ‘young’ obj only exists in small numbers
Object exists long in the app does not usually reference to newly created objs: a cache needs clean up
When objects get GCed?
1) Obj not referenced from anywhere else
2) isolation of island(A -> B. B-> C. C->A)
System.gc()
1) not force GC to happen
2) just a hit to trigger a GC
GC steps
1) MARK - from root node(main()), walk through the object graph, marks objects reachable as live
2) SWEEP - delete unreachable objs
3) COMPACTING- compacting the memory, moveing love objs, making the allocation contiguous rather than fragmented(slow).
How heap is divided for GC?
1) Young Generation
Eden Space - new objs are created, when full, a minor GC triggered
Survivor 1 - all survived Objs from minor GC + All objs in Survivor 2, swap with S2
Survivor 2
2) Old Generation
after survive N minor GC, objs moves to Old generation
Minor Vs Major GC?
1) Minor GC runs in young generation
2) Majoy GC runs across the heap
3) They all stop the word for some time
Process of Minor GC?
Every time copy all live obj to the empty S
1) New Objs Allocated in Eden
2) Eden is full, minor GC, move live obj to S1
3) Eden is full again, minor GC, move live to S2, also copy all S1 live obj to S2(avoid compacting)
4) Eden is full, minor GC, copy all live obj to S1, all S2 live obj to S1, swap s1, s2
when any obj have survive N GC, move them to the old generation
Deep Copy Vs shallow Copy?
1) Shallow Copy:
primitive types: values are copies
Objs: only reference are copied - any change made to the objs will impact the original obj
2) Deep Copy
clone object and original object will be 100% disjoint.
Change made to the cloned one will not impact the original one
3) java.lang.Object clone() method - provide a shallow copy. Deep copy need to override Clone() method
Process of Major GC?
1) Triggered when old Gen is almost full
2) Runs across both young/old - whole heap
3) Time consuming, cause app halt
JVM performance measures
Responsiveness/Latency
How long does it take to handle a request
Throughput
Amount of work done by the app over some time
Different types of GC?
- Serial GC
- Runs in single thread, STW for whole process - Concurrent GC
- Performs GC along with App thread
- only STW on MARK/RE-MARK, SWEEP/COMPATING can happen concurrently
- Use when there are high amounts of CPU/memory, app demands short pauses
- CMS, G1(java7) - Parallel GC
- multi-threaded GC process
- not run concurrently with App, SWT for the whole process
- Use when there is low amounts of cpu/memory, app demands high throughput and can withstand pauses
When G1 is the new replacement of CMS?
Divides heap in to small regions, region with the most garbage will get collected first
- more predictable for GC pauses
- Parallel and concurrent together
- low pauses with fragmentation
- Better heap utilisation
JVM Parameters for GC
- XX:+UseSerialGC: young/old use serial GC
- XX:+UseParallelGC: young - parallel, old - serial
- XX:+UseParNewGC: only young parallel
- XX:+UseParallelOldGC: both young/old parallel
- XX:+UseConcMarkSweepGC: young useParNewGC, old use CMS GC
- XX:+UseG1GC
default: parallel 1.6, G1 since 1.7
finalize()
will be called before GC only once per obj
not gauranteed to be GCed immediately.