0% found this document useful (0 votes)
66 views

Unit 5: Destructors, Memory Management and Garbage Collection

In unit 4, you learnt about methods, constructors and how both methods and constructors are implemented using Java. In this unit, you will be introduced to destructors, which is the opposite of constructors, memory management and garbage collection.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Unit 5: Destructors, Memory Management and Garbage Collection

In unit 4, you learnt about methods, constructors and how both methods and constructors are implemented using Java. In this unit, you will be introduced to destructors, which is the opposite of constructors, memory management and garbage collection.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Domasi College of Education

Unit 5

Destructors, Memory
Management and Garbage
Collection

Introduction
In unit 4, you learnt about methods, constructors and how
both methods and constructors are implemented using
Java.
In this unit, you will be introduced to destructors, which is
the opposite of constructors, memory management and
garbage collection.

Computer Programming II, Object-Oriented 5-1


Domasi College of Education

Success Criteria
By the end of this unit, you should be able to:
 describe a garbage collector
 state types of garbage collectors
 explain how destructors work
 differentiate between a constructor and a destructor
 describe the meaning of memory management
 implement the destructor using Java code

Key Words
You will find the following key words or phrases in this unit:
destructor, garbage collector, heap, young generation and
old generation. Watch for these and make sure that you
understand what they mean and how they are used in the
unit.

Destructors
You should note that in Java, when we create an
object of the class it occupies some space in the
memory (heap). If we do not delete these objects, it
remains in the memory and occupies unnecessary
space that is not upright from the aspect of
programming. To resolve this problem, we use the
destructor.

However, in Java there is no such concept as a


destructor. As an alternate option to the destructor in
Java, we use finalize() method as a destructor.

Java also provides the garbage collector that works the


same as the destructor. The garbage collector is a
program (thread) that runs on the Java Virtual
Machine (JVM). It automatically deletes the unused
objects (objects that are no longer used) and free-up
the memory. Therefore, in this case, garbage collection is
process of reclaiming the runtime unused memory
automatically. In other words, it is a way to destroy the
unused objects.

How Does Destructor Work in Java?

Computer Programming II, Object-Oriented 5-2


Domasi College of Education

The destructor has a finalize() method in Java.


When the objects are created, they are stored in the
heap memory. These are accessible by main or child
threads. So when these objects are no more used by
the main thread or its child threads, they become
eligible for garbage collection and the memory which
was acquired now becomes available by new objects
being created. Before an object is a garbage collected
by the garbage collector, the JRE (Java Runtime
Environment) calls the finalize() method to close the
input-output streams, the database connections,
network connections, etc.

So, the process of allocating memory to new Java


objects and de-allocating memory from unused objects
is called memory management. In Java, memory
management happens automatically and memory
allocation and de-allocation happen in the
background.

You may be interested to note that destructor is the


opposite of constructor which has been discussed in
unit 4.

Advantages of Destructor
 It releases the resources occupied by the object.
 No explicit call is required.
 It does not accept any parameter and cannot be
overloaded.

Java finalize() Method


The syntax of the finalize() method is as follows:

protected void finalize throws Throwable()


{
//resources to be closed
}

Here is an example of a destructor:

public class DestructorExample

Computer Programming II, Object-Oriented 5-3


Domasi College of Education

{
public static void main(String[] args)
{
DestructorExample de = new
DestructorExample ();
de.finalize();
de = null;
System.gc();
System.out.println("Inside the main()
method");
}
protected void finalize()
{
System.out.println("Object is destroyed
by the Garbage Collector");
}
}

Differences between a constructor and a destructor

Constructor Destructor
 A destructor is used
 A constructor is used
to delete or destroy
to initialize an
the objects when they
instance of a class
are no longer in use
 Constructors are  Destructors are
called when an called when an object
instance of a class is is destroyed or
created released
 Memory allocation  Releases the memory
 Overloading is  Overloading is not
possible allowed
 No arguments can be
 They are allowed to
passed in a
have arguments
destructor

Types of garbage collectors

Computer Programming II, Object-Oriented 5-4


Domasi College of Education

 Serial Garbage Collector: It uses the mark and


sweeps approach for young generation and old
generation.
 Parallel Garbage Collector: It is similar to Serial
Garbage Collector except that, it spawns N (the
number of CPU cores in the system) threads for
young generation garbage collection.
 Parallel Old Garbage Collector: It is similar to
Parallel Garbage Collector, except that it uses
multiple threads for both generations.
 Concurrent Mark Sweep (CMS) Collector: It
does the garbage collection for the old
generation. It is also known as Concurrent Low
Pause Collector.
 G1 Garbage Collector: It introduced in Java 7.
Its objective is to replace the CMS collector. It is
a parallel, concurrent, and CMS collector. There
is no young and old generation space. It divides
the heap into several equal sized heaps. It first
collects the regions with lesser live data.

Advantages of Garbage Collection

 It makes Java memory efficient because


garbage collector removes the unreferenced
objects from heap memory.
 It is automatically done by the garbage
collector (a part of JVM) so we don't need to
make extra efforts.

You may take note that Java Virtual Machine (JVM)


uses a mark and sweep algorithm used to determine
which objects are no longer in use by the application.
This is a two-step process:
1. The algorithm scans all object references and
marks those objects which are found as alive.
2. It sweeps or reclaims all the heap memory that is
not occupied by these marked objects.

Computer Programming II, Object-Oriented 5-5


Domasi College of Education

Self-Evaluation Activity 5
1. What is a destructor?
2. Give any two differences between constructor and
destructor.
3. Describe Serial Garbage Collector.

Summary
In this unit, you have been introduced to destructors, memory
management and garbage collection.
 Destructor is used to de-allocate memory allocated by
objects.
 Memory management is the process of allocating memory to
new Java objects and de-allocating memory from unused
objects.
 Garbage collection is a way to destroy the unused objects.

Reflection
How would you implement a destructor using finalize()
method? What have you found more challenging
during your implementation?

Computer Programming II, Object-Oriented 5-6


Domasi College of Education

Unit Test
1. How does destructor work in Java?
2. State two advantages of garbage collection.
3. Why don't Java objects have destructors?
4. What is memory management?
5. In Java, would you like to see an explicit
destructor? Explain.

Computer Programming II, Object-Oriented 5-7


Domasi College of Education

Answers to Unit 5 Activity


Answers to Activity 5
1. Destructor is used to de-allocate memory
allocated by objects.
2. Choose any two:
 A constructor is used to initialize an
instance of a class while a destructor
is used to delete or destroy the objects
when they are no longer in use
 Constructors are called when an
instance of a class is created while
destructors are called when an object is
destroyed or released
 Memory allocation is done by
constructors while destructors release
the memory
 Overloading is possible in constructors
while in destructors overloading is not
allowed
 Constructors are allowed to have
arguments while in destructors
arguments cannot be passed
3. Serial Garbage Collector uses the mark and
sweeps approach for young generation and old
generation.

Answers to Unit 5 Test


1. When the objects are created, they are stored in the
heap memory. These are accessible by main or child
threads. So when these objects are no more used by
the main thread or its child threads, they become
eligible for garbage collection and the memory which
was acquired now becomes available by new objects
being created.
2. Here are the possible responses:
 It makes Java memory efficient
 It is automatically done by the garbage
collector.

Computer Programming II, Object-Oriented 5-8


Domasi College of Education

3. Java manages memory using built-in garbage


collector.
4. Memory management is the process of allocating
memory to new Java objects and de-allocating
memory from unused objects.
5. Yes, a gc method to explicitly call garbage collection
immediately on the object. It will provide better
flexibility over application that requires optimal
usage of memory.

Computer Programming II, Object-Oriented 5-9

You might also like