Mca I Yr. (Aiml) - Sem - I: Course Name: Object Oriented Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

MCA I Yr.

(AIML)- SEM – I

Course Name: Object Oriented Programming

Dr. Sachin Upadhye


Department of Computer Science and Applications
School of Computer Science and Engineering
RBU, Nagpur
[email protected], 9970094109
SYLLABUS OF SEMESTER - I,
MCA (MASTER OF COMPUTER APPLICATIONS)
(Artificial Intelligence and Machine Learning)

Course : Object Oriented Programming


Total Credits : 4
L: 3 Hrs, T: 0 Hr, P: 2 Hr, Per Week
Garbage Collection, finalize() Method
What is Garbage Collection?
• In C/C++, a programmer is responsible for both the creation and
destruction of objects. Usually, programmer neglects the
destruction of useless objects.
• Due to this negligence, at a certain point, sufficient memory may
not be available to create new objects, and the entire program will
terminate abnormally, causing OutOfMemoryErrors.
But in Java, the programmer need not care for all those objects which are no
longer in use. Garbage collector destroys these objects. The main objective of
Garbage Collector is to free heap memory by destroying unreachable objects.
How Does Garbage Collection in Java works?
• Java garbage collection is an automatic process. 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 or unreferenced object is no longer referenced by any part of your


program. So the memory used by an unreferenced object can be reclaimed.
The programmer does not need to mark objects to be deleted explicitly. The
garbage collection implementation lives in the JVM.
Important Concepts Related to Garbage Collection in Java
An object is said to be unreachable if it doesn’t contain any reference to it. Also,
note that objects which are part of the island of isolation are also unreachable.
An object is said to be eligible for GC(garbage collection) if it is unreachable.
After i = null, integer object 4 in the heap area is suitable for garbage collection
in the above image.

Ways to make an object eligible for Garbage Collector

Even though the programmer is not responsible for destroying useless objects but
it is highly recommended to make an object unreachable(thus eligible for GC) if
it is no longer required.
There are generally four ways to make an object eligible for garbage collection.
1.Nullifying the reference variable
2.Re-assigning the reference variable
3.An object created inside the method
4.Island of Isolation
Ways for requesting JVM to run Garbage Collector
Once we make an object eligible for garbage collection, it may not destroy
immediately by the garbage collector. Whenever JVM runs the Garbage
Collector program, then only the object will be destroyed. But when JVM runs
Garbage Collector, we can not expect.
We can also request JVM to run Garbage Collector. There are two ways to do it :

• Using System.gc() method: System class contain static method gc() for requesting JVM
to run Garbage Collector.

• Using Runtime.getRuntime().gc() method: Runtime class allows the application to


interface with the JVM in which the application is running. Hence by using its gc()
method, we can request JVM to run Garbage Collector.

• There is no guarantee that any of the above two methods will run Garbage Collector

• The call System.gc() is effectively equivalent to the call : Runtime.getRuntime().gc()


Finalization
Just before destroying an object, Garbage Collector calls finalize() method on the
object to perform cleanup activities. Once finalize() method completes, Garbage
Collector destroys that object.
finalize() method is present in Object class with the following prototype.

Based on our requirement, we can override finalize() method for performing our
cleanup activities like closing connection from the database.
• The finalize() method is called by Garbage Collector, not JVM. However,
Garbage Collector is one of the modules of JVM.

• The finalize() method is never invoked more than once for any object.

• Object class finalize() method has an empty implementation. Thus, it is


recommended to override the finalize() method to dispose of system resources
or perform other cleanups.
Advantages of Garbage Collection in Java
The advantages of Garbage Collection in Java are:

•It makes java memory-efficient because the 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 extra effort.
Real-World Example
Suppose you go for the internship at Company, and you were told to write a
program to count the number of employees working in the company(excluding
interns). To make this program, you have to use the concept of a garbage
collector.
A program to create a class called Employee having the following data
members.
1.An ID for storing unique id allocated to every employee.
2. Name of employee.
3. age of an employee.

Also, provide the following methods:


1.A parameterized constructor to initialize name and age. The ID should be
initialized in this constructor.
2.A method show() to display ID, name, and age.
3.A method showNextId() to display the ID of the next employee.
s
Now to get the correct output:
• Now garbage collector(gc) will see 2 objects free. Now to decrement
nextId,gc(garbage collector) will call method to finalize() only when we
programmers have overridden it in our class.
• we have to write the following 3 steps before closing brace of sub-block.
• Set references to null(i.e X = Y = null;)
• Call, System.gc();
• Call, System.runFinalization();
s
s

You might also like