• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java GC
 

Java GC

on

  • 2,313 views

 

Statistics

Views

Total Views
2,313
Views on SlideShare
2,313
Embed Views
0

Actions

Likes
3
Downloads
77
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

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

    Java GC Java GC Presentation Transcript

    • Java Garbage Collection
    • Agenda
      Introduction to GC
      Introduction to GC Algorithms
      Finalize() in Java
      Reference type in Java
    • Purpose
      Know how GC works
      Avoid traps when works with GC
      Enhance program performance
    • Agenda
      Introduction to GC
      Introduction to GC Algorithms
      Finalize() in Java
      Reference type in Java
    • Introduction to GC
      Garbage Collection
      Used to release non-used memories
      Java specification doesn’t define detail of GC, each company can implement its own GC
      Different JVM has different GC algorithm
      Sun – Solaris
      IBM – AIX

    • Introduction to GC
      GC is a overhead, system has to stop current execution to execute GC
      cause short stop and may influence user experience
      android team make it as short as possible
      we can do nothing to improve GC but improve our program
    • Stack & Heap memory
      Instance variables and Objects lie on Heap
      Local variables and methods lie on the Stack
      Each running thread has its Stack
      C++ has local objects (object in stack)
      All Java objects live in Heap
    • Stack & Heap memory
      public static void main() {
      int a;
      People Rae = new People();
      People Ear = new People();
      Rae.father = Ear;
      }
    • Stack & Heap memory
      Stack
      Heap
      People
      Ear
      Rae
      People
      a
    • Introduction to GC
      GC happens in Heap memory only
    • Stack & Heap memory
    • Stack & Heap memory
    • Agenda
      Introduction to GC
      Introduction to GC Algorithms
      Finalize() in Java
      Reference type in Java
    • Introduction to GC Algorithms
      Different JVM has different GC implementations
      Reference counting
      Mark and Sweep
      Stop and Copy
      Adaptive

    • Reference counting
      If a object is referenced, the counter is increased by 1
      If reference to a object is removed, then the counter is decreased by 1
      GC will collect these object whose counter is 0
    • Reference counting - drawback
      Each object should maintain a counter
      Can’t identify circular condition
      Stack
      Heap
      People(2)
      A(1)
      Ear
      B(1)
      C(1)
      Rae
      People(1)
      a
    • Mark and Sweep
      Three phases
      Mark phase
      Sweep phase
      Compact phase
    • Reachable Object
      Root : the beginning of all reference
      reference from main()
      reference from static method()

      if a object can be visited by a serious of reference from Root, it is called reachable, otherwise it is unreachable
      unreachable objects will be collected by GC
    • Garbage detection is ordinarily accomplished by defining a set of roots and determining reachability from the roots.
      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.
      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.
    • Mark phase
      There are two method to mark reachable objects
      Tracing
      Reference counting
    • Tracing
      From Roots, search all reachable objects and mark them
      avoid circular reference
      Stack
      Heap
      People
      A
      Ear
      B
      C
      Rae
      People
      a
    • Sweep phase
      After mark phase, these which not be referenced are not marked
      GC will release their memory
    • Compact phase
      After several GCs, Heap may contain fragments
      Need to be rearranged
      Two algorithms
      Compacting
      Copying

    • Compacting
      move objects in Heap from one end to another end
    • Copying
      Copy objects from one Heap to another Heap
      Heap A
      Heap B
    • Mark and Sweep
      Avoid circular reference
      Have to manage memory fragments
    • Stop and Copy
      Copy live object to another Heap and leave deads
      Heap A
      Heap B
    • Stop and Copy
      Need not to manage memory fragments
      Double memory space needed
    • Adaptive
      GC has more than one strategy to deal with garbage collection
      GC can change strategy during garbage collection depending on heap status
    • final() method in Object
      It is GC that execute final in every object
    • each thread of execution has its own stack.
    • Two basic approaches to distinguishing live objects from garbage are reference counting and tracing.
    • 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.
    • A disadvantage of reference counting is that it does not detect cycles.
    • 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.
    • Compacting collectors
      Two strategies commonly used by mark and sweep collectors are compacting and copying.
      heap fragmentation
    • Agenda
      Introduction to GC
      Introduction to GC Algorithms
      Finalize() in Java
      Reference type in Java
    • finalize()
      Called before the object's memory is being reclaimed by the VM.
      The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required.
      Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.
      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.
    • finalize()
      If one object override its finalize(), this object is called finalizable
      If a object doesn’t override its finalize(), when GC collect it, its memory is freed directly
      If a object is finalizable, when GC collect it, this object will be send into a queue, and its finalize() will then be executed
      After finalize() been successfully executed, then it will be release in next GC
    • finalize()
      If a object is referenced by an finalizable object, it will be released after the finalizable object is released
    • Finalizable - Drawback
      All finalizable need to be executed by an independent thread, but the priority of this is not high
      May keep too much unused object in Heap
      If its finalize() does not execute corrected (return Exception), then this object will never be released
      All objects it refer to will never be released
    • What can I do to improve GC
      finalize() is supposed to be used to release memory only (ex. native code)
      Set obj = null whenever this object is no longer used
      Some Java objects provide reusable objects, use them instead of creating new one (ex. Thread pool)
      Do not override finalize() if really not necessary
    • Agenda
      Introduction to GC
      Introduction to GC Algorithms
      Finalize() in Java
      Reference type in Java
    • Reference Type in Java
      Reference type associates with GC
      There are four kind of references in Java
      Strong reference
      Soft reference
      Weak reference
      Phantom reference
    • Reference Type in Java
      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.
    • Strong Reference
      Object obj = new Object();
      GC can not free Strong reachable object until there are no more reference to this object
    • Soft Reference
      Not a solid reference
      When Memory is not enough, GC can release Soft reachable objects
      Good implement to data cache
    • Weak Reference
      Weaker than Soft reference
      Every time when GC starts, weak reachable objects are collected
      Disposable objects
    • Weak Reference
      Once the garbage collector decides that an object obj is is weakly-reachable, the following happens:
      A set ref of references is determined. ref contains the following elements:
      All weak references pointing to obj.
      All weak references pointing to objects from which obj is either strongly or softly reachable.
      All references in ref are atomically cleared.
      All objects formerly being referenced by ref become eligible for finalization.
      At some future point, all references in ref will be enqueued with their corresponding reference queues, if any.
    • Phantom Reference
      Weakest reference
      Work with ReferenceQueue class
      The PhantomReference class is useful only to track the impending collection of the referring object.
      When the garbage collector determines an object is phantomly reachable, the PhantomReference object is placed on its ReferenceQueue.
      The PhantomReference object referred to has been finalized and is ready to be collected.
    • Phantom Reference
      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.
    • Thank you