SlideShare ist ein Scribd-Unternehmen logo
1 von 40
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

Weitere ähnliche Inhalte

Was ist angesagt?

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.pptVarshini62
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Marcos García
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
Introduction to Garbage Collection
Introduction to Garbage CollectionIntroduction to Garbage Collection
Introduction to Garbage CollectionArtur Mkrtchyan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javajunnubabu
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 

Was ist angesagt? (20)

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java I/O
Java I/OJava I/O
Java I/O
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.ppt
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java IO
Java IOJava IO
Java IO
 
Exception handling
Exception handlingException handling
Exception handling
 
Introduction to Garbage Collection
Introduction to Garbage CollectionIntroduction to Garbage Collection
Introduction to Garbage Collection
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 

Andere mochten auch

Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Ashish Jha
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage CollectionAzul Systems Inc.
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage CollectionHaim Yadid
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection TechniquesAn Khuong
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展Leon Chen
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlLeon Chen
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage CollectionDoug Hawkins
 
Java GC Tuning
Java GC TuningJava GC Tuning
Java GC Tuningpprun
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
 
Jvm基础调优实践(v1.0)
Jvm基础调优实践(v1.0)Jvm基础调优实践(v1.0)
Jvm基础调优实践(v1.0)ddviplinux
 
Java GC - Pause tuning
Java GC - Pause tuningJava GC - Pause tuning
Java GC - Pause tuningekino
 
Java gc
Java gcJava gc
Java gcNiit
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Anna Shymchenko
 

Andere mochten auch (20)

Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
How long can you afford to Stop The World?
How long can you afford to Stop The World?How long can you afford to Stop The World?
How long can you afford to Stop The World?
 
JVM及其调优
JVM及其调优JVM及其调优
JVM及其调优
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission Control
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
 
Java GC Tuning
Java GC TuningJava GC Tuning
Java GC Tuning
 
enums
enumsenums
enums
 
Gc algorithms
Gc algorithmsGc algorithms
Gc algorithms
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Jvm基础调优实践(v1.0)
Jvm基础调优实践(v1.0)Jvm基础调优实践(v1.0)
Jvm基础调优实践(v1.0)
 
About garbage collection
About garbage collectionAbout garbage collection
About garbage collection
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
Java GC - Pause tuning
Java GC - Pause tuningJava GC - Pause tuning
Java GC - Pause tuning
 
Java gc
Java gcJava gc
Java gc
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
 

Ähnlich wie Java GC

Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad ideaPVS-Studio
 
Garbage collection 介紹
Garbage collection 介紹Garbage collection 介紹
Garbage collection 介紹kao kuo-tung
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Carl Brown
 
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxGarbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxPavanKumarPNVS
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMjaganmohanreddyk
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdfSudhanshiBakre1
 
Java Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelJava Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelErnesto Arroyo Ron
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android ApplicationsLokesh Ponnada
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaKonrad Malawski
 
Java reference objects basic
Java reference objects   basicJava reference objects   basic
Java reference objects basicIsaac Liao
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Maarten Balliauw
 
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaAdvanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaXamarin
 

Ähnlich wie Java GC (20)

Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
Garbage collection 介紹
Garbage collection 介紹Garbage collection 介紹
Garbage collection 介紹
 
Memory management
Memory managementMemory management
Memory management
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)
 
C# basics
C# basicsC# basics
C# basics
 
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxGarbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
Java Garbage Collector and The Memory Model
Java Garbage Collector and The Memory ModelJava Garbage Collector and The Memory Model
Java Garbage Collector and The Memory Model
 
.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves.NET UY Meetup 7 - CLR Memory by Fabian Alves
.NET UY Meetup 7 - CLR Memory by Fabian Alves
 
RxJava@DAUG
RxJava@DAUGRxJava@DAUG
RxJava@DAUG
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
Gc in android
Gc in androidGc in android
Gc in android
 
My Rmi F
My Rmi FMy Rmi F
My Rmi F
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Garbage Collection In Micorosoft
Garbage Collection In  MicorosoftGarbage Collection In  Micorosoft
Garbage Collection In Micorosoft
 
Java reference objects basic
Java reference objects   basicJava reference objects   basic
Java reference objects basic
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaAdvanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
 

Kürzlich hochgeladen

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Kürzlich hochgeladen (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

Java GC

  • 2. Agenda Introduction to GC Introduction to GC Algorithms Finalize() in Java Reference type in Java
  • 3. Purpose Know how GC works Avoid traps when works with GC Enhance program performance
  • 4. Agenda Introduction to GC Introduction to GC Algorithms Finalize() in Java Reference type in Java
  • 5. 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 …
  • 6. 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
  • 7. 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
  • 8. Stack & Heap memory public static void main() { int a; People Rae = new People(); People Ear = new People(); Rae.father = Ear; }
  • 9. Stack & Heap memory Stack Heap People Ear Rae People a
  • 10. Introduction to GC GC happens in Heap memory only
  • 11. Stack & Heap memory
  • 12. Stack & Heap memory
  • 13. Agenda Introduction to GC Introduction to GC Algorithms Finalize() in Java Reference type in Java
  • 14. Introduction to GC Algorithms Different JVM has different GC implementations Reference counting Mark and Sweep Stop and Copy Adaptive …
  • 15. 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
  • 16. 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
  • 17. Mark and Sweep Three phases Mark phase Sweep phase Compact phase
  • 18. 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
  • 19. 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.
  • 20. Mark phase There are two method to mark reachable objects Tracing Reference counting
  • 21. Tracing From Roots, search all reachable objects and mark them avoid circular reference Stack Heap People A Ear B C Rae People a
  • 22. Sweep phase After mark phase, these which not be referenced are not marked GC will release their memory
  • 23. Compact phase After several GCs, Heap may contain fragments Need to be rearranged Two algorithms Compacting Copying …
  • 24. Compacting move objects in Heap from one end to another end
  • 25. Copying Copy objects from one Heap to another Heap Heap A Heap B
  • 26. Mark and Sweep Avoid circular reference Have to manage memory fragments
  • 27. Stop and Copy Copy live object to another Heap and leave deads Heap A Heap B
  • 28. Stop and Copy Need not to manage memory fragments Double memory space needed
  • 29. Adaptive GC has more than one strategy to deal with garbage collection GC can change strategy during garbage collection depending on heap status
  • 30. final() method in Object It is GC that execute final in every object
  • 31. each thread of execution has its own stack.
  • 32. Two basic approaches to distinguishing live objects from garbage are reference counting and tracing.
  • 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.
  • 34. A disadvantage of reference counting is that it does not detect cycles.
  • 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.
  • 36. Compacting collectors Two strategies commonly used by mark and sweep collectors are compacting and copying. heap fragmentation
  • 37. Agenda Introduction to GC Introduction to GC Algorithms Finalize() in Java Reference type in Java
  • 38. 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.
  • 39. 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
  • 40. finalize() If a object is referenced by an finalizable object, it will be released after the finalizable object is released
  • 41. 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
  • 42. 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
  • 43. Agenda Introduction to GC Introduction to GC Algorithms Finalize() in Java Reference type in Java
  • 44. 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
  • 45. 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.
  • 46. Strong Reference Object obj = new Object(); GC can not free Strong reachable object until there are no more reference to this object
  • 47. Soft Reference Not a solid reference When Memory is not enough, GC can release Soft reachable objects Good implement to data cache
  • 48. Weak Reference Weaker than Soft reference Every time when GC starts, weak reachable objects are collected Disposable objects
  • 49. 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.
  • 50. 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.
  • 51. 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.