SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances (20)

Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Java constructors
Java constructorsJava constructors
Java constructors
 
garbage collector
garbage collectorgarbage collector
garbage collector
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
OOP java
OOP javaOOP java
OOP java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Java string handling
Java string handlingJava string handling
Java string handling
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java threads
Java threadsJava threads
Java threads
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 

En vedette

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
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection TechniquesAn Khuong
 
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
 

En vedette (20)

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)
 
Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
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
 

Similaire à 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
 

Similaire à 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)
 

Plus 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
 

Plus 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
 

Dernier

Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
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
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 

Dernier (20)

Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
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)
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 

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