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)

JVM
JVMJVM
JVM
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java threads
Java threadsJava threads
Java threads
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Exception handling
Exception handlingException handling
Exception handling
 
Java package
Java packageJava package
Java package
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading 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)
 
Features of java
Features of javaFeatures of java
Features of java
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 

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
 

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...
 
Java GC
Java GCJava GC
Java GC
 
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
 

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

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
 
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
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 

Dernier (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
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...
 
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
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 

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