html5-img
1 / 45

Garbage collector

Garbage collector. … an intr o duction Peter Varsanyi. Agenda. Garbage Collector. Basics. Garbage collector. What is it? Find data objects in the program, that cannot be accessed in the future Reclaim resources used by these objects. Basics. Advantages? Dangling pointer bugs

necia
Télécharger la présentation

Garbage collector

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Garbage collector … an introduction Peter Varsanyi Confidential

  2. Agenda Confidential

  3. Garbage Collector Basics Confidential

  4. Garbage collector What is it? Find data objects in the program, that cannot be accessed in the future Reclaim resources used by these objects Basics Confidential

  5. Advantages? Dangling pointer bugs Double free bugs Certain type of memory leaks Basics Confidential

  6. Disadvantages Extra computing resources Unpredictable collection time More memory-related work than useful work Basics Confidential

  7. Automatic garbage collector First automatic garbage collection: LISP(1958) Several other languages implemented it: • BASIC(1964) • Logo(1967) • Java 1.0(1996) Brief history Confidential

  8. Garbage collector Memory management Confidential

  9. OS level process 32 bit: 4GB address space 64 bit: 16EB address space Kernel space User space Memory management 0 GB 2 GB 4 GB 0x0 0x40000000 0xC0000000 0xFFFFFFFF 0x80000000 Confidential

  10. OS level process OS + C runtime JVM/Native heap Java Heap Memory management (-Xmx2GB) 0 GB 2 GB 4 GB Java heap OS + C runtime JVM NativeHeap 0x0 0x40000000 0xC0000000 0xFFFFFFFF 0x80000000 Confidential

  11. Conservative collector • Any bit pattern can be a pointer • No compiler cooperation required Precise collector • Require compiler cooperation • Can move objects Commercial JVMs use precise collector Memory management Confidential

  12. Reference types Strong Soft Weak Phantom Memory management Confidential

  13. Memory management Confidential

  14. Object Lifecycle Memory management Confidential

  15. Garbage collector Types Confidential

  16. Reference counting collector An early garbage collection strategy Reference count is maintained in the object Overhead to increment/decrement reference count Cannot clean cyclic references Garbage collector Confidential

  17. Tracing collector Trace out the heap starting from roots Two phases: • Mark live objects • Clean dead objects Garbage collector types Confidential

  18. Mark Find objects in the heap “Paint” them Non-marked objects are dead Work is linear to “live set” Garbage collection Confidential

  19. Sweep Scans heap for dead objects Work is linear to heap size Garbage collection Confidential

  20. Compact Avoid fragmentation Relocates objects Remap: fix all references to live objects End of heap is a large contagious free area Work is linear to “live set” Garbage collection Confidential

  21. Copy Moves all objects Single pass operation Split memory into 2 region Work is linear to “live set” Garbage collection Confidential

  22. Mark/Sweep (1.0) Easy to implement Reclaim cyclic structures Java Garbage collectors Confidential

  23. Generational collection (1.2) Young/Eden generation Tenured generation Perm generation Java Garbage collectors Confidential

  24. Generational collection Java Garbage collectors Confidential

  25. Separate collectors Minor GC • When eden is full • Sweeps eden + current survivor Major GC • When old is full • Perm + tenured collected Java Garbage collectors Confidential

  26. Java garbage collectors Eden S1 S2 Tenured Confidential

  27. Java Garbage collectors Confidential

  28. Remembered set Track references into young gen Card marking Java Garbage collectors Confidential

  29. Mark/Compact(1.3) Combines copy & mark/sweep Mark live objects, move these Java Garbage collectors Confidential

  30. Concepts Parallel • Uses multiple CPU to perform collection at the same time Concurrent • Performs GC concurrently with application’s own execution Safe point • Point in thread execution, where collector can identify all references in thread’s execution stack Java Garbage collectors Confidential

  31. Serial GC Default with 1 CPU Young collector: Copy Old collector: Mark/Sweep/Compact Faster allocation Java Garbage collectors Confidential

  32. Parallel GC Multiple threads to collect young More than 2 CPU pause time will be reduced Old gen collected with parallel mark/sweep Java Garbage collectors Confidential

  33. CMS(Concurrent Mark Sweep) Multiple threads to collect young Tenured generation: mark/sweep Does not compact or copy Low pause time Occupancy fraction Java Garbage collectors Confidential

  34. CMS phases Initial mark(STW) Concurrent mark Concurrent preclean Remark(STW) Concurrent sweep Concurrent reset Java garbage collectors Confidential

  35. CMS advantages/disadvantages Advantages: • Low latency Disadvantages • Cannot work when old is full • No compaction Java garbage collectors Confidential

  36. G1 to the rescue Most empty region first Estimate region clean time User defined target time Java Garbage collectors Confidential

  37. Java Garbage collectors Confidential

  38. G1 to the rescue Heap partitioned into equal size of chunks More predictable GC pauses High throughput Compacting collector Java Garbage collectors Confidential

  39. G1 phases Initial mark(STW) Root region scanning Concurrent marking Remark(STW)(SATB) Clean up Copy(STW) Java Garbage collectors Confidential

  40. Creating object has high cost Do not create lots of small objects No more memory leak(MAGIC!) Increase the heap, what could happen? Null everything you don’t use System.gc() will collect everything immediately Myths Confidential

  41. Confidential

  42. Tools Jstat(Part of JDK) Java VisualVM(Oracle JDK) • Visual GC plugin -XX:+PrintGC -XX:+PrintGCDetails Confidential

  43. http://cscircles.cemc.uwaterloo.ca/java_visualize/ http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/ http://www.slideshare.net/cnbailey/memory-efficient-java Useful links Confidential

  44. Confidential

  45. Questions? No I will not clean your room. Q/A Confidential

More Related