Gc algorithms
Upcoming SlideShare
Loading in...5
×
 

Gc algorithms

on

  • 1,298 views

Garbage Collection Algorithms

Garbage Collection Algorithms

Statistics

Views

Total Views
1,298
Views on SlideShare
1,242
Embed Views
56

Actions

Likes
4
Downloads
56
Comments
0

1 Embed 56

https://twitter.com 56

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Gc algorithms Gc algorithms Presentation Transcript

  • GC Algorithms Michał Warecki
  • Who am I? ● Programming Geek interested in: – GC – JiT Compilers – Concurrency – Non-blocking algorithms – Programming languages runtime
  • Outline ● Introduction ● Detecting dead objects ● Basic algorithms ● Generational GC ● Multi-threaded GC ● Real-time GC
  • What I'm not covering ● GC tuning ● JVM GC Options ● JVM Specific GC Implementation -Xms8g -Xmx8g -XX:MaxPermSize=256m -XX:NewSize=3G -XX:MaxNewSize=3g -XX:NewRatio=4 -XX:MaxTenuringThreshold=5 -XX:+UseConcMarkSweepGC -XX:+CMSScavengeBeforeRemark -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gclogs.txt -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:ParallelGCThreads=7 -XX:+UseGCTaskAffinity -XX:+BindGCTaskThreadsToCPUs -XX:+UnlockDiagnosticVMOptions -XX:ParGCCardsPerStrideChunk=32768
  • Why do I need to know about GC? Because GC stops your application!
  • Why to collect garbage? ● Limited storage ● Programmers do not like to get dirty ● Programmers make mistakes – Too little collected – memory leaks – error – Too much collected – broken programs – error ● Programmers like good software design – Explicit memory management conflicts with the software engineering principles of abstraction and modularity
  • What is garbage? Garbage is an object which does not carry any reference from other objects.
  • Detecting dead objects Reference tracing vs Reference counting
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference tracing Root references HEAP
  • Reference counting (naive) New(): ref ← allocate() if ref = null error “Out of memory” rc(ref) ← 0 return ref atomic Write(src, i, ref): addReference(ref) deleteReference(src[i]) src[i] ← ref addReference(ref): if ref != null rc(ref) ← rc(ref) + 1 deleteReference(ref): if ref != null rc(ref) ← rc(ref) – 1 if rc(ref) = 0 for each fld in Pointers(ref) deleteReference(*fld) free(ref) The Garbage Collection Handbook – Jones, Hosking, Moss
  • Reference counting (naive) Root references HEAP
  • Reference counting (naive) Root references 1 1 1 2 3 0 1 1 1 1 HEAP
  • Reference counting (naive) Root references 1 1 1 1 1 0 1 1 1 1 HEAP
  • Reference counting (naive) Root references 1 1 1 1 1 0 1 1 1 1 HEAP Memory leak!
  • Detecting dead objects ● Reference tracing ✔ No mutator overhead ✔ Collect cycles ✔ High throughput ✗ Batch style ✗ Not real time ● Reference counting ✔ Incremental ✔ Short pause ✔ Real time ✗ Reference cycles ✗ High mutator overhead ✗ Low throughput
  • Basic tracing algorithms Not Moving Moving Mark/Sweep Mark/Compact Copying
  • Mark/Sweep vs Mark/Compact Before collection After Mark/Sweep After Mark/Compact Live object Free space Dead object
  • Mark/Sweep vs Mark/Compact After Mark/Sweep After Mark/Compact Free list allocation Bump the pointer allocation
  • Mark/Sweep vs Mark/Compact ● Mark/Sweep ✔ Fast ✗ Fragmentation ✗ Slower free list allocation ● Mark/Compact ✗ Slow ✔ Compacted heap ✔ Fast bump the pointer allocation
  • Copying GC (Ping Pong) ToFrom
  • Copying GC (Ping Pong) ToFrom
  • Copying GC (Ping Pong) To From
  • Copying GC (Survivor spaces) Eden From To Eden From To Eden From To Eden From To
  • Copying GC (Survivor spaces) Eden From To Eden To From Eden To From Eden From To
  • Copying GC ✔ Compacted heap ✔ The speed depends on the number of live objects ✔ Possible improvement of locality during evacuation ✗ Space overhead
  • Generational hypothesis Weak generational hypothesis is the observation that, in most cases, young objects are much more likely to die than old objects. Conversely any object that has survived several GC cycles will probably survive a lot more.
  • Generational GC Young space Old space 4 10 16 16 Objects headers Object age Max tenuring threashold = 15
  • Generational GC algorithms Young space Old space Copying GC Mark/Sweep Mark/Compact Reference counting
  • Dynamic Generational GC G1 GC Eden Survivor Old Humongous Unused
  • Card Table and Remembered Set Young space Old space Card Table Dirty Dirty card – write to memory – possible reference to young generation. Will be added to Remembered Set
  • Card Table Ref picture: http://blog.ragozin.info/2011/06/understanding-gc-pauses-in-jvm-hotspots.html
  • Multi-threaded GC Mutator GC Serial GC Parallel GC Concurrent GC Incremental GC
  • Thread Local Allocation Buffer ● Thread allocates within TLAB using bump the pointer ● Improved objects locality ● No contention single pointer TLAB
  • Promotion Local Allocation Buffer Each thread has two PLABs: • One for survivor space, • One for tenured space PLAB1 PLAB2 GC Thread 1 GC Thread 2
  • Real-time GC Plane: I'm landing. GC: Pff, please wait 2 minutes, I'm collecting
  • Real-time GC Real-time systems impose operational deadlines on particular tasks within an application. These real-time tasks must be able to response to application inputs (events) within a fixed time window. The Garbage Collection Handbook – Jones, Hosking, Moss
  • Metronome GC Traditional GC Metronome GC
  • Thanks!! Questions?