0% found this document useful (0 votes)
15 views13 pages

Garbage Collection

The document discusses automatic garbage collection in Java, explaining how it identifies and deletes unreferenced objects in heap memory. It outlines the marking and normal deletion processes, as well as the use of the Mark & Sweep algorithm by the JVM's garbage collector. Additionally, it describes how objects can become unreferenced and the roles of the finalize() and gc() methods in garbage collection.

Uploaded by

poornasandur18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Garbage Collection

The document discusses automatic garbage collection in Java, explaining how it identifies and deletes unreferenced objects in heap memory. It outlines the marking and normal deletion processes, as well as the use of the Mark & Sweep algorithm by the JVM's garbage collector. Additionally, it describes how objects can become unreferenced and the roles of the finalize() and gc() methods in garbage collection.

Uploaded by

poornasandur18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

OOPS with Java

Dr. Poorna Chandra S PhD


Associate Professor
Dept of MCA
Jain College of Engineering, Belagavi

January 5, 2024

1 / 12
Automatic Garbage Collection I
Automatic garbage collection is the process of looking at
heap memory, identifying which objects are in use and
which are not, and deleting the unused objects. An in use
object, or a referenced object, means that some part of
your program still maintains a pointer to that object.

An unused object, or unreferenced object, is no longer


referenced by any part of your program. So the memory
used by an unreferenced object can be reclaimed.

2 / 12
Automatic Garbage Collection II

In a programming language like C, allocating and


deallocating memory is a manual process.
In Java, process of deallocating memory is handled
automatically by the garbage collector.
The basic process can be described as follows.
Step 1: Marking
The first step in the process is called marking. This is
where the garbage collector identifies which pieces of
memory are in use and which are not.

3 / 12
Automatic Garbage Collection III

4 / 12
Referenced objects are shown in blue.
Unreferenced objects are shown in gold.
All objects are scanned in the marking phase to make this
determination.
This can be a very time consuming process if all objects
in a system must be scanned.
Step 2: Normal Deletion
Normal deletion removes unreferenced objects leaving
referenced objects and pointers to free space.

5 / 12
6 / 12
Step 2a: Deletion with Compacting
To further improve performance, in addition to deleting
unreferenced objects, you can also compact the
remaining referenced objects.
By moving referenced object together, this makes new
memory allocation much easier and faster.

7 / 12
8 / 12
The Garbage Collector in the JVM uses Mark & Sweep
Algorithm to free the memory from unused objects.

How can an object be unreferenced?


By nulling the reference
By assigning a reference to another
By anonymous object etc.
1) By nulling a reference
Employee e=new Employee();
e=null;

9 / 12
2) By assigning a reference to another:
Employee e1=new Employee();
Employee e2=new Employee();
e1=e2;//now the first object referred by e1 is available
for garbage collection
3) By anonymous object: new Employee();
finalize() method
The finalize() method is invoked each time before the
object is garbage collected. This method can be used to
perform cleanup processing.
This method is defined in Object class as:
protected void finalize()

10 / 12
gc() method
The gc() method is used to invoke the garbage collector
to perform cleanup processing.
The gc() is found in System and Runtime classes.
public static void gc()

11 / 12
——

12 / 12
p u b l i c c l a s s TestGarbage1 {
public void f i n a l i z e ( ) {
System . o u t . p r i n t l n ( ” o b j e c t i s garbage
collected ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g args [ ] ) {
TestGarbage1 s1=new TestGarbage1 ( ) ;
TestGarbage1 s2=new TestGarbage1 ( ) ;
s1= n u l l ;
s2= n u l l ;
System . gc ( ) ;
}
}

12 / 12

You might also like