SlideShare uma empresa Scribd logo
1 de 15
GARBAGE
COLLECTION
MEMORY MANAGEMENT
• Languages like C or C++ that do not offer automatic
garbage collection.
• Creating code that performs manual memory
management cleanly and thoroughly is a nontrivial
and complex task, and while estimates vary, it is
arguable that manual memory management can
double the development effort for a complex
program.
 Java's garbage collector provides an automatic
solution to memory management. It frees you from
having to add any memory management logic to
your application. It helps in ensuring program
integrity.
OVERVIEW
 Garbage collection revolves around making sure
that the heap has as much free space as possible.
 Heap is that part of memory where Java objects
live, and it's the one and only part of memory that is
in any way involved in the garbage collection
process.
 When the garbage collector runs, its purpose is to
find and delete objects that cannot be reached.
 If no more memory is available for the heap, then
the new operator throws an
OutOfMemoryException.
DISADVANTAGE
 GC is a overhead, system has to stop current
execution to execute it .
1. Causes short stop and may influence user’s
experience.
2. We can do nothing to improve gc,but only improve
out program
1. Less control over scheduling of CPU time.
WHEN DOES GARBAGE COLLECTOR RUN
 The garbage collector is under the control of the
JVM. The JVM decides when to run the garbage
collector.
 Objects that are referenced are said to be alive.
Unreferenced objects are considered
dead(garbage).
 From within your Java program you can ask the
JVM to run the garbage collector, but there are no
guarantees, under any circumstances, that the
JVM will comply.
 The JVM will typically run the garbage collector
when it senses that memory is running low.
DETECTING GARBAGE OBJECTS
 REFERENCE-COUNTING COLLECTORS: When
the object is created the reference count of object is
set to one. When you reference the object r.c is
incremented by 1. When reference to an object
goes out of scope ,the r.c is decremented
 Object that have r.c zero (not referenced) is a
garbage object.
 ADVANTAGES:
1. Can run in small chunks of time.
2. Suitable for real time environments
 TRACING COLLECTOR(mark and sweep algo)
1. Traverse through a graph ,starting from the root
2. Marks the objects that are reachable.
3. At the end of the trace all unmarked objects are
identified as garbage.
 COMPACTING COLLECTORS :
1. Reduces fragmentation of memory by moving all free
space to one side during garbage collection.
2. Free memory is then available to be used by other
objects.
3. References to shifted objects are updated to refer to
new m/m location.
EXPLICITLY MAKING OBJECTS AVAILABLE
FOR GARBAGE COLLECTION
 Nulling a Reference : Set the reference variable that
refers to the object to null.
1. public class GarbageTruck {
2. public static void main(String [] args) {
3. StringBuffer sb = new StringBuffer("hello");
4. System.out.println(sb);
5. // The StringBuffer object is not eligible for collection
6. sb = null;
7. // Now the StringBuffer object is eligible for collection
8. }
9. }
 Reassigning a Reference Variable : We can also
decouple a reference variable from an object by setting
the reference variable to refer to another object.
class GarbageTruck {
public static void main(String [] args) {
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("goodbye");
System.out.println(s1);
// At this point the StringBuffer "hello" is not eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection
}
}
FORCING GARBAGE COLLECTION
Garbage collection cannot be forced. However, Java
provides some methods that allow you to request that
the JVM perform garbage collection.
 USING RUNTIME CLASS
1. The garbage collection routines that Java provides are
members of the Runtime class.
2. The Runtime class is a special class that has a single
object (a Singleton) for each main program.
3. The Runtime object provides a mechanism for
communicating directly with the virtual machine.
4. To get the Runtime instance, you can use the method
Runtime.getRuntime(), which returns the Singleton.
Once you have the Singleton you can invoke the
garbage collector using the gc() method.
 Using static methods
1. The simplest way to ask for garbage collection
(remember—just a request) is
System.gc();
Garbage collection process is not under the user's control. So it
makes no sense to call System.gc(); explicitly. It entirely
depends on the JVM.
JVM also runs parallel gc threads to remove unused objects
from memory . So there is no requirement to explicitly
call System.gc() or Runtime.gc() method.
FINALIZATION
 Java provides you a mechanism to run some code just
before your object is deleted by the garbage collector.
This code is located in a method named finalize() that all
classes inherit from class Object.
 Any code that you put into your class's overridden
finalize() method is not guaranteed to run.
 There are a couple of concepts concerning finalize() that
you need to remember.
1. For any given object, finalize() will be called only once
(at most) by the garbage collector.
2. Calling finalize() can actually result in saving an object
from deletion.
 Right before an asset is freed, the java run time
calls the finalize() method on the object.
 The finalize method has this general form :
protected void finalize()
{
//finalize code
}
 Keyword protected prevents access to finalize by
code defined outside its class.
THANK YOU
 Somya Bagai
 Sonia Kukreja
 Varun Luthra

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Applets
AppletsApplets
Applets
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
JVM
JVMJVM
JVM
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
Java package
Java packageJava package
Java package
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
Deadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbianDeadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbian
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java Streams
Java StreamsJava Streams
Java Streams
 

Destaque

Garbage collection algorithms
Garbage collection algorithmsGarbage collection algorithms
Garbage collection algorithmsachinth
 
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.
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVMaragozin
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryachinth
 
Garbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika LangerGarbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika LangerJAXLondon_Conference
 
G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?C2B2 Consulting
 
G1 collector and tuning and Cassandra
G1 collector and tuning and CassandraG1 collector and tuning and Cassandra
G1 collector and tuning and CassandraChris Lohfink
 
GC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerGC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerMonica Beckwith
 
Qualities of good technical writing along with comparison between technical a...
Qualities of good technical writing along with comparison between technical a...Qualities of good technical writing along with comparison between technical a...
Qualities of good technical writing along with comparison between technical a...muhammad ilyas
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage CollectionHaim Yadid
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage CollectionDoug Hawkins
 
controlling the vibration of automobile suspension system using pid controller
controlling the vibration of automobile suspension system using pid controllercontrolling the vibration of automobile suspension system using pid controller
controlling the vibration of automobile suspension system using pid controllersiva kumar
 
Machine Learning for Non-technical People
Machine Learning for Non-technical PeopleMachine Learning for Non-technical People
Machine Learning for Non-technical Peopleindico data
 
Exhaust system
Exhaust systemExhaust system
Exhaust systemSaoud Al-k
 

Destaque (19)

Garbage collection algorithms
Garbage collection algorithmsGarbage collection algorithms
Garbage collection algorithms
 
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
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVM
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Garbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika LangerGarbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika Langer
 
G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?
 
Water Resources
Water ResourcesWater Resources
Water Resources
 
G1 collector and tuning and Cassandra
G1 collector and tuning and CassandraG1 collector and tuning and Cassandra
G1 collector and tuning and Cassandra
 
GC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerGC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance Engineer
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
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?
 
Qualities of good technical writing along with comparison between technical a...
Qualities of good technical writing along with comparison between technical a...Qualities of good technical writing along with comparison between technical a...
Qualities of good technical writing along with comparison between technical a...
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
 
Plastic waste management
Plastic waste management Plastic waste management
Plastic waste management
 
controlling the vibration of automobile suspension system using pid controller
controlling the vibration of automobile suspension system using pid controllercontrolling the vibration of automobile suspension system using pid controller
controlling the vibration of automobile suspension system using pid controller
 
Machine Learning for Non-technical People
Machine Learning for Non-technical PeopleMachine Learning for Non-technical People
Machine Learning for Non-technical People
 
Exhaust system
Exhaust systemExhaust system
Exhaust system
 

Semelhante a Garbage collection

Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdfSudhanshiBakre1
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android ApplicationsLokesh Ponnada
 
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
 
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxGarbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxPavanKumarPNVS
 
performance optimization: Memory
performance optimization: Memoryperformance optimization: Memory
performance optimization: Memory晓东 杜
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsPhillip Koza
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMjaganmohanreddyk
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningCarol McDonald
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Maarten Balliauw
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuningosa_ora
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performanceRoger Xia
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx javaCongTrung Vnit
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Prashanth Kumar
 

Semelhante a Garbage collection (20)

Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
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
 
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptxGarbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
Garbagecollectionhgfhgfhgfhgfgkfkjfkjkjfkjfjhfg.pptx
 
performance optimization: Memory
performance optimization: Memoryperformance optimization: Memory
performance optimization: Memory
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
GC in C#
GC in C#GC in C#
GC in C#
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
Thread in java.pptx
Thread in java.pptxThread in java.pptx
Thread in java.pptx
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
Memory management
Memory managementMemory management
Memory management
 
Gc in android
Gc in androidGc in android
Gc in android
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx java
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
Javasession10
Javasession10Javasession10
Javasession10
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 

Mais de Somya Bagai

Hotspots of biodiversity
Hotspots of biodiversityHotspots of biodiversity
Hotspots of biodiversitySomya Bagai
 
Evolution & structure of erp
Evolution & structure of erpEvolution & structure of erp
Evolution & structure of erpSomya Bagai
 
Random scan displays and raster scan displays
Random scan displays and raster scan displaysRandom scan displays and raster scan displays
Random scan displays and raster scan displaysSomya Bagai
 
Method of least square
Method of least squareMethod of least square
Method of least squareSomya Bagai
 
Least square method
Least square methodLeast square method
Least square methodSomya Bagai
 
Push down automata
Push down automataPush down automata
Push down automataSomya Bagai
 

Mais de Somya Bagai (6)

Hotspots of biodiversity
Hotspots of biodiversityHotspots of biodiversity
Hotspots of biodiversity
 
Evolution & structure of erp
Evolution & structure of erpEvolution & structure of erp
Evolution & structure of erp
 
Random scan displays and raster scan displays
Random scan displays and raster scan displaysRandom scan displays and raster scan displays
Random scan displays and raster scan displays
 
Method of least square
Method of least squareMethod of least square
Method of least square
 
Least square method
Least square methodLeast square method
Least square method
 
Push down automata
Push down automataPush down automata
Push down automata
 

Último

4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 

Último (20)

4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 

Garbage collection

  • 2. MEMORY MANAGEMENT • Languages like C or C++ that do not offer automatic garbage collection. • Creating code that performs manual memory management cleanly and thoroughly is a nontrivial and complex task, and while estimates vary, it is arguable that manual memory management can double the development effort for a complex program.  Java's garbage collector provides an automatic solution to memory management. It frees you from having to add any memory management logic to your application. It helps in ensuring program integrity.
  • 3. OVERVIEW  Garbage collection revolves around making sure that the heap has as much free space as possible.  Heap is that part of memory where Java objects live, and it's the one and only part of memory that is in any way involved in the garbage collection process.  When the garbage collector runs, its purpose is to find and delete objects that cannot be reached.  If no more memory is available for the heap, then the new operator throws an OutOfMemoryException.
  • 4. DISADVANTAGE  GC is a overhead, system has to stop current execution to execute it . 1. Causes short stop and may influence user’s experience. 2. We can do nothing to improve gc,but only improve out program 1. Less control over scheduling of CPU time.
  • 5. WHEN DOES GARBAGE COLLECTOR RUN  The garbage collector is under the control of the JVM. The JVM decides when to run the garbage collector.  Objects that are referenced are said to be alive. Unreferenced objects are considered dead(garbage).  From within your Java program you can ask the JVM to run the garbage collector, but there are no guarantees, under any circumstances, that the JVM will comply.  The JVM will typically run the garbage collector when it senses that memory is running low.
  • 6. DETECTING GARBAGE OBJECTS  REFERENCE-COUNTING COLLECTORS: When the object is created the reference count of object is set to one. When you reference the object r.c is incremented by 1. When reference to an object goes out of scope ,the r.c is decremented  Object that have r.c zero (not referenced) is a garbage object.  ADVANTAGES: 1. Can run in small chunks of time. 2. Suitable for real time environments
  • 7.  TRACING COLLECTOR(mark and sweep algo) 1. Traverse through a graph ,starting from the root 2. Marks the objects that are reachable. 3. At the end of the trace all unmarked objects are identified as garbage.
  • 8.  COMPACTING COLLECTORS : 1. Reduces fragmentation of memory by moving all free space to one side during garbage collection. 2. Free memory is then available to be used by other objects. 3. References to shifted objects are updated to refer to new m/m location.
  • 9. EXPLICITLY MAKING OBJECTS AVAILABLE FOR GARBAGE COLLECTION  Nulling a Reference : Set the reference variable that refers to the object to null. 1. public class GarbageTruck { 2. public static void main(String [] args) { 3. StringBuffer sb = new StringBuffer("hello"); 4. System.out.println(sb); 5. // The StringBuffer object is not eligible for collection 6. sb = null; 7. // Now the StringBuffer object is eligible for collection 8. } 9. }
  • 10.  Reassigning a Reference Variable : We can also decouple a reference variable from an object by setting the reference variable to refer to another object. class GarbageTruck { public static void main(String [] args) { StringBuffer s1 = new StringBuffer("hello"); StringBuffer s2 = new StringBuffer("goodbye"); System.out.println(s1); // At this point the StringBuffer "hello" is not eligible s1 = s2; // Redirects s1 to refer to the "goodbye" object // Now the StringBuffer "hello" is eligible for collection } }
  • 11. FORCING GARBAGE COLLECTION Garbage collection cannot be forced. However, Java provides some methods that allow you to request that the JVM perform garbage collection.  USING RUNTIME CLASS 1. The garbage collection routines that Java provides are members of the Runtime class. 2. The Runtime class is a special class that has a single object (a Singleton) for each main program. 3. The Runtime object provides a mechanism for communicating directly with the virtual machine. 4. To get the Runtime instance, you can use the method Runtime.getRuntime(), which returns the Singleton. Once you have the Singleton you can invoke the garbage collector using the gc() method.
  • 12.  Using static methods 1. The simplest way to ask for garbage collection (remember—just a request) is System.gc(); Garbage collection process is not under the user's control. So it makes no sense to call System.gc(); explicitly. It entirely depends on the JVM. JVM also runs parallel gc threads to remove unused objects from memory . So there is no requirement to explicitly call System.gc() or Runtime.gc() method.
  • 13. FINALIZATION  Java provides you a mechanism to run some code just before your object is deleted by the garbage collector. This code is located in a method named finalize() that all classes inherit from class Object.  Any code that you put into your class's overridden finalize() method is not guaranteed to run.  There are a couple of concepts concerning finalize() that you need to remember. 1. For any given object, finalize() will be called only once (at most) by the garbage collector. 2. Calling finalize() can actually result in saving an object from deletion.
  • 14.  Right before an asset is freed, the java run time calls the finalize() method on the object.  The finalize method has this general form : protected void finalize() { //finalize code }  Keyword protected prevents access to finalize by code defined outside its class.
  • 15. THANK YOU  Somya Bagai  Sonia Kukreja  Varun Luthra