Unit 5: Destructors, Memory Management and Garbage Collection
Unit 5: Destructors, Memory Management and Garbage Collection
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.
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.
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.
{
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");
}
}
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
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?
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.