java gc
TRANSCRIPT
![Page 1: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/1.jpg)
Java Garbage Collection
![Page 2: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/2.jpg)
Agenda
• Introduction to GC• Introduction to GC Algorithms• Finalize() in Java• Reference type in Java
![Page 3: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/3.jpg)
Purpose
• Know how GC works• Avoid traps when works with GC• Enhance program performance
![Page 4: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/4.jpg)
Agenda
• Introduction to GC• Introduction to GC Algorithms• Finalize() in Java• Reference type in Java
![Page 5: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/5.jpg)
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– …
![Page 6: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/6.jpg)
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
![Page 7: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/7.jpg)
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
![Page 8: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/8.jpg)
Stack & Heap memory
public static void main() { int a; People Rae = new People(); People Ear = new People(); Rae.father = Ear; }
![Page 9: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/9.jpg)
Stack & Heap memory
a
Rae
Ear
Stack Heap
People
People
![Page 10: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/10.jpg)
Introduction to GC
• GC happens in Heap memory only
![Page 11: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/11.jpg)
Agenda
• Introduction to GC• Introduction to GC Algorithms• Finalize() in Java• Reference type in Java
![Page 12: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/12.jpg)
Introduction to GC Algorithms
• Different JVM has different GC implementations– Reference counting– Mark and Sweep– Stop and Copy– Adaptive– …
![Page 13: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/13.jpg)
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
![Page 14: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/14.jpg)
Reference counting - drawback
• Each object should maintain a counter• Can’t identify circular condition
a
Rae
Ear
Stack Heap
People(2)
People(1)
A(1)
B(1) C(1)
![Page 15: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/15.jpg)
Mark and Sweep
• Three phases– Mark phase– Sweep phase– Compact phase
![Page 16: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/16.jpg)
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
![Page 17: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/17.jpg)
Tracing
• From Roots, search all reachable objects and mark them– avoid circular reference
a
Rae
Ear
Stack Heap
People
People
A
B C
![Page 18: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/18.jpg)
Sweep phase
• After mark phase, these which not be referenced are not marked
• GC will release their memory
![Page 19: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/19.jpg)
Compact phase
• After several GCs, Heap may contain fragments
• Need to be rearranged• Two algorithms– Compacting– Copying– …
![Page 20: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/20.jpg)
Compacting
• move objects in Heap from one end to another end
obj A
obj B
obj B
obj A
![Page 21: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/21.jpg)
Copying
• Copy objects from one Heap to another Heap
obj A
obj B
obj C
Heap A
obj A
obj B
obj C
Heap B
![Page 22: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/22.jpg)
Mark and Sweep
• Avoid circular reference• Have to manage memory fragments
![Page 23: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/23.jpg)
Stop and Copy
• Copy live object to another Heap and leave deads
obj A
obj B
obj C
Heap A
obj A
obj C
Heap B
![Page 24: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/24.jpg)
Stop and Copy
• Need not to manage memory fragments• Double memory space needed
![Page 25: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/25.jpg)
Adaptive
• GC has more than one strategy to deal with garbage collection
• GC can change strategy during garbage collection depending on heap status
![Page 26: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/26.jpg)
Agenda
• Introduction to GC• Introduction to GC Algorithms• Finalize() in Java• Reference type in Java
![Page 27: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/27.jpg)
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.
![Page 28: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/28.jpg)
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
![Page 29: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/29.jpg)
finalize()
• If a object is referenced by an finalizable object, it will be released after the finalizable object is released
![Page 30: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/30.jpg)
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
![Page 31: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/31.jpg)
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
![Page 32: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/32.jpg)
Agenda
• Introduction to GC• Introduction to GC Algorithms• Finalize() in Java• Reference type in Java
![Page 33: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/33.jpg)
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
![Page 34: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/34.jpg)
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.
![Page 35: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/35.jpg)
Strong Reference
• Object obj = new Object();• GC can not free Strong reachable object until
there are no more reference to this object
![Page 36: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/36.jpg)
Soft Reference
• Not a solid reference• When Memory is not enough, GC can release
Soft reachable objects• Good implement to data cache
![Page 37: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/37.jpg)
Weak Reference
• Weaker than Soft reference• Every time when GC starts, weak reachable
objects are collected• Disposable objects
![Page 38: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/38.jpg)
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.
![Page 39: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/39.jpg)
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.
![Page 40: Java GC](https://reader036.vdocument.in/reader036/viewer/2022062513/5563a4dcd8b42a2b6a8b51da/html5/thumbnails/40.jpg)
THANK YOU