Java GC


Published on

Published in: Education

Java GC

  1. 1. Java Garbage Collection<br />
  2. 2. Agenda<br />Introduction to GC<br />Introduction to GC Algorithms<br />Finalize() in Java<br />Reference type in Java<br />
  3. 3. Purpose<br />Know how GC works<br />Avoid traps when works with GC<br />Enhance program performance <br />
  4. 4. Agenda<br />Introduction to GC<br />Introduction to GC Algorithms<br />Finalize() in Java<br />Reference type in Java<br />
  5. 5. Introduction to GC<br />Garbage Collection<br />Used to release non-used memories<br />Java specification doesn’t define detail of GC, each company can implement its own GC<br />Different JVM has different GC algorithm<br />Sun – Solaris<br />IBM – AIX<br />…<br />
  6. 6. Introduction to GC<br />GC is a overhead, system has to stop current execution to execute GC<br />cause short stop and may influence user experience<br />android team make it as short as possible<br />we can do nothing to improve GC but improve our program<br />
  7. 7. Stack & Heap memory<br />Instance variables and Objects lie on Heap<br />Local variables and methods lie on the Stack<br />Each running thread has its Stack<br />C++ has local objects (object in stack)<br />All Java objects live in Heap<br />
  8. 8. Stack & Heap memory<br />public static void main() {<br />int a;<br /> People Rae = new People();<br /> People Ear = new People();<br />Rae.father = Ear; <br />}<br />
  9. 9. Stack & Heap memory<br />Stack<br />Heap<br />People<br />Ear<br />Rae<br />People<br />a<br />
  10. 10. Introduction to GC<br />GC happens in Heap memory only<br />
  11. 11. Stack & Heap memory<br />
  12. 12. Stack & Heap memory<br />
  13. 13. Agenda<br />Introduction to GC<br />Introduction to GC Algorithms<br />Finalize() in Java<br />Reference type in Java<br />
  14. 14. Introduction to GC Algorithms<br />Different JVM has different GC implementations<br />Reference counting<br />Mark and Sweep<br />Stop and Copy<br />Adaptive<br />…<br />
  15. 15. Reference counting<br />If a object is referenced, the counter is increased by 1<br />If reference to a object is removed, then the counter is decreased by 1<br />GC will collect these object whose counter is 0<br />
  16. 16. Reference counting - drawback<br />Each object should maintain a counter<br />Can’t identify circular condition<br />Stack<br />Heap<br />People(2)<br />A(1)<br />Ear<br />B(1)<br />C(1)<br />Rae<br />People(1)<br />a<br />
  17. 17. Mark and Sweep<br />Three phases<br />Mark phase<br />Sweep phase<br />Compact phase<br />
  18. 18. Reachable Object<br />Root : the beginning of all reference<br />reference from main()<br />reference from static method()<br />…<br />if a object can be visited by a serious of reference from Root, it is called reachable, otherwise it is unreachable<br />unreachable objects will be collected by GC<br />
  19. 19. Garbage detection is ordinarily accomplished by defining a set of roots and determining reachability from the roots.<br />An object is reachable if there is some path of references from the roots by which the executing program can access the object. The roots are always accessible to the program. <br />Any objects that are reachable from the roots are considered live. Objects that are not reachable are considered garbage, because they can no longer affect the future course of program execution.<br />
  20. 20. Mark phase<br />There are two method to mark reachable objects<br />Tracing<br />Reference counting <br />
  21. 21. Tracing<br />From Roots, search all reachable objects and mark them<br />avoid circular reference <br />Stack<br />Heap<br />People<br />A<br />Ear<br />B<br />C<br />Rae<br />People<br />a<br />
  22. 22. Sweep phase<br />After mark phase, these which not be referenced are not marked<br />GC will release their memory<br />
  23. 23. Compact phase<br />After several GCs, Heap may contain fragments<br />Need to be rearranged<br />Two algorithms<br />Compacting<br />Copying<br />…<br />
  24. 24. Compacting<br />move objects in Heap from one end to another end<br />
  25. 25. Copying<br />Copy objects from one Heap to another Heap<br />Heap A<br />Heap B<br />
  26. 26. Mark and Sweep<br />Avoid circular reference<br />Have to manage memory fragments<br />
  27. 27. Stop and Copy<br />Copy live object to another Heap and leave deads<br />Heap A<br />Heap B<br />
  28. 28. Stop and Copy<br />Need not to manage memory fragments<br />Double memory space needed<br />
  29. 29. Adaptive<br />GC has more than one strategy to deal with garbage collection<br />GC can change strategy during garbage collection depending on heap status<br />
  30. 30. final() method in Object<br />It is GC that execute final in every object <br />
  31. 31. each thread of execution has its own stack.<br />
  32. 32. Two basic approaches to distinguishing live objects from garbage are reference counting and tracing.<br />
  33. 33. Reference counting garbage collectors distinguish live objects from garbage objects by keeping a count for each object on the heap. The count keeps track of the number of references to that object. Tracing garbage collectors, on the other hand, actually trace out the graph of references starting with the root nodes. Objects that are encountered during the trace are marked in some way. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected.<br />
  34. 34. A disadvantage of reference counting is that it does not detect cycles.<br />
  35. 35. Some Java objects have finalizers, others do not. Objects with finalizers that are left unmarked after the sweep phase must be finalized before they are freed. Unmarked objects without finalizers may be freed immediately unless referred to by an unmarked finalizable object. All objects referred to by a finalizable object must remain on the heap until after the object has been finalized.<br />
  36. 36. Compacting collectors<br />Two strategies commonly used by mark and sweep collectors are compacting and copying.<br />heap fragmentation<br />
  37. 37. Agenda<br />Introduction to GC<br />Introduction to GC Algorithms<br />Finalize() in Java<br />Reference type in Java<br />
  38. 38. finalize()<br />Called before the object's memory is being reclaimed by the VM.<br />The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required.<br />Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored. <br />Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. <br />
  39. 39. finalize()<br />If one object override its finalize(), this object is called finalizable<br />If a object doesn’t override its finalize(), when GC collect it, its memory is freed directly<br />If a object is finalizable, when GC collect it, this object will be send into a queue, and its finalize() will then be executed<br />After finalize() been successfully executed, then it will be release in next GC<br />
  40. 40. finalize()<br />If a object is referenced by an finalizable object, it will be released after the finalizable object is released<br />
  41. 41. Finalizable - Drawback<br />All finalizable need to be executed by an independent thread, but the priority of this is not high<br />May keep too much unused object in Heap <br />If its finalize() does not execute corrected (return Exception), then this object will never be released<br />All objects it refer to will never be released<br />
  42. 42. What can I do to improve GC<br />finalize() is supposed to be used to release memory only (ex. native code)<br />Set obj = null whenever this object is no longer used<br />Some Java objects provide reusable objects, use them instead of creating new one (ex. Thread pool)<br />Do not override finalize() if really not necessary<br />
  43. 43. Agenda<br />Introduction to GC<br />Introduction to GC Algorithms<br />Finalize() in Java<br />Reference type in Java<br />
  44. 44. Reference Type in Java<br />Reference type associates with GC<br />There are four kind of references in Java<br />Strong reference<br />Soft reference<br />Weak reference<br />Phantom reference<br />
  45. 45. Reference Type in Java<br />Strongly reachable: An object that can be accessed by a strong reference.Softly reachable: An object that is not strongly reachable and can be accessed through a soft reference.Weakly reachable: An object that is not strongly or softly reachable and can be accessed through a weak reference.Phantomly reachable: An object that is not strongly, softly, or weakly reachable, has been finalized, and can be accessed through a phantom reference.<br />
  46. 46. Strong Reference<br />Object obj = new Object();<br />GC can not free Strong reachable object until there are no more reference to this object<br />
  47. 47. Soft Reference<br />Not a solid reference<br />When Memory is not enough, GC can release Soft reachable objects<br />Good implement to data cache<br />
  48. 48. Weak Reference<br />Weaker than Soft reference<br />Every time when GC starts, weak reachable objects are collected<br />Disposable objects<br />
  49. 49. Weak Reference<br />Once the garbage collector decides that an object obj is is weakly-reachable, the following happens: <br />A set ref of references is determined. ref contains the following elements: <br />All weak references pointing to obj. <br />All weak references pointing to objects from which obj is either strongly or softly reachable. <br />All references in ref are atomically cleared. <br />All objects formerly being referenced by ref become eligible for finalization. <br />At some future point, all references in ref will be enqueued with their corresponding reference queues, if any. <br />
  50. 50. Phantom Reference<br />Weakest reference<br />Work with ReferenceQueue class<br />The PhantomReference class is useful only to track the impending collection of the referring object.<br />When the garbage collector determines an object is phantomly reachable, the PhantomReference object is placed on its ReferenceQueue.<br />The PhantomReference object referred to has been finalized and is ready to be collected.<br />
  51. 51. Phantom Reference<br />Phantom references are useful for implementing cleanup operations that are necessary before an object gets garbage-collected. They are sometimes more flexible than the finalize() method.<br />
  52. 52. Thank you<br />