Gc algorithms

1,822 views

Published on

Garbage Collection Algorithms

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,822
On SlideShare
0
From Embeds
0
Number of Embeds
60
Actions
Shares
0
Downloads
93
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Gc algorithms

  1. 1. GC Algorithms Michał Warecki
  2. 2. Who am I? ● Programming Geek interested in: – GC – JiT Compilers – Concurrency – Non-blocking algorithms – Programming languages runtime
  3. 3. Outline ● Introduction ● Detecting dead objects ● Basic algorithms ● Generational GC ● Multi-threaded GC ● Real-time GC
  4. 4. 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
  5. 5. Why do I need to know about GC? Because GC stops your application!
  6. 6. 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
  7. 7. What is garbage? Garbage is an object which does not carry any reference from other objects.
  8. 8. Detecting dead objects Reference tracing vs Reference counting
  9. 9. Reference tracing Root references HEAP
  10. 10. Reference tracing Root references HEAP
  11. 11. Reference tracing Root references HEAP
  12. 12. Reference tracing Root references HEAP
  13. 13. Reference tracing Root references HEAP
  14. 14. Reference tracing Root references HEAP
  15. 15. Reference tracing Root references HEAP
  16. 16. Reference tracing Root references HEAP
  17. 17. Reference tracing Root references HEAP
  18. 18. Reference tracing Root references HEAP
  19. 19. Reference tracing Root references HEAP
  20. 20. Reference tracing Root references HEAP
  21. 21. Reference tracing Root references HEAP
  22. 22. Reference tracing Root references HEAP
  23. 23. Reference tracing Root references HEAP
  24. 24. Reference tracing Root references HEAP
  25. 25. Reference tracing Root references HEAP
  26. 26. Reference tracing Root references HEAP
  27. 27. Reference tracing Root references HEAP
  28. 28. Reference tracing Root references HEAP
  29. 29. 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
  30. 30. Reference counting (naive) Root references HEAP
  31. 31. Reference counting (naive) Root references 1 1 1 2 3 0 1 1 1 1 HEAP
  32. 32. Reference counting (naive) Root references 1 1 1 1 1 0 1 1 1 1 HEAP
  33. 33. Reference counting (naive) Root references 1 1 1 1 1 0 1 1 1 1 HEAP Memory leak!
  34. 34. 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
  35. 35. Basic tracing algorithms Not Moving Moving Mark/Sweep Mark/Compact Copying
  36. 36. Mark/Sweep vs Mark/Compact Before collection After Mark/Sweep After Mark/Compact Live object Free space Dead object
  37. 37. Mark/Sweep vs Mark/Compact After Mark/Sweep After Mark/Compact Free list allocation Bump the pointer allocation
  38. 38. Mark/Sweep vs Mark/Compact ● Mark/Sweep ✔ Fast ✗ Fragmentation ✗ Slower free list allocation ● Mark/Compact ✗ Slow ✔ Compacted heap ✔ Fast bump the pointer allocation
  39. 39. Copying GC (Ping Pong) ToFrom
  40. 40. Copying GC (Ping Pong) ToFrom
  41. 41. Copying GC (Ping Pong) To From
  42. 42. Copying GC (Survivor spaces) Eden From To Eden From To Eden From To Eden From To
  43. 43. Copying GC (Survivor spaces) Eden From To Eden To From Eden To From Eden From To
  44. 44. Copying GC ✔ Compacted heap ✔ The speed depends on the number of live objects ✔ Possible improvement of locality during evacuation ✗ Space overhead
  45. 45. 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.
  46. 46. Generational GC Young space Old space 4 10 16 16 Objects headers Object age Max tenuring threashold = 15
  47. 47. Generational GC algorithms Young space Old space Copying GC Mark/Sweep Mark/Compact Reference counting
  48. 48. Dynamic Generational GC G1 GC Eden Survivor Old Humongous Unused
  49. 49. 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
  50. 50. Card Table Ref picture: http://blog.ragozin.info/2011/06/understanding-gc-pauses-in-jvm-hotspots.html
  51. 51. Multi-threaded GC Mutator GC Serial GC Parallel GC Concurrent GC Incremental GC
  52. 52. Thread Local Allocation Buffer ● Thread allocates within TLAB using bump the pointer ● Improved objects locality ● No contention single pointer TLAB
  53. 53. 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
  54. 54. Real-time GC Plane: I'm landing. GC: Pff, please wait 2 minutes, I'm collecting
  55. 55. 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
  56. 56. Metronome GC Traditional GC Metronome GC
  57. 57. Thanks!! Questions?

×