This document discusses garbage collection in Java. It defines garbage collection as the automatic process of freeing memory from objects that are no longer referenced. It describes the key steps of marking and deleting unreachable objects. The document outlines different garbage collection algorithms like reference counting, tracing, and compacting. It explains how objects become eligible for collection by getting de-referenced. The document also discusses the finalize method in Java that allows for cleanup before an object is collected.
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Automatic Garbage Collection in Java
1.
2. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
3. Garbage Collection in Java
Abhishek H Menon
abhishekmenon121989@gmail.
com
www.facebook.com/abhishe
kmenon5
twitter.com/abhishekmenon
121989
in.linkedin.com/in/abhishekh
menon
4. Features of Java programming language
• Platform independent
• Object oriented
• Automatic garbage collection
• Rich standard library
5. Automatic garbage collection
• Process of looking at heap memory, identifying which objects
are in use and which are not, and deleting the unused objects.
• In programming languages like C, CPP allocating and deallocating
is a manual process, but in Java garbage collector automatically
do allocation and deallocation.
• For performing this task of garbage collection, there are two
steps
– Marking
– Normal deletion
– Deletion with compacting
7. Normal Deletion
Memory allocator holds a list of references to free
spaces, and searches for free spaces and allocated
memory sequentially then on.
Memory space
8. Deletion with compacting
After normal
deletion with
compacting
Memory allocation holds the reference to the
beginning of free space and allocated memory
sequentially then on
9. Garbage collection
• What is Garbage collection?
– Memory management technique
– Process of freeing objects
– No longer referenced by the program
10. Garbage collection
• Why Garbage collection?
– Free unreferenced objects.
– Relieves programmer from manual freeing the
memory.
– Helps in ensuring program integrity.
– Disadvantages
• Extra Overhead.
• Less control over scheduling of CPU time.
11. Garbage collection in java
• Purpose?
– Find and delete unreachable objects.
– Free space as much as possible.
• When?
– GC is under control of JVM.
– One can request but no guarantees.
• How?
– Discovery of unreachable objects.
– With the help of Algorithms.
12. Garbage collection in java
• Ways of making an object eligible for
collection
– Nulling a reference
– Reassigning a reference variable
– Isolating a reference variable
• Forcing Garbage collection
– Methods available to perform GC
– Only requests and no demands
– Using runtime class
– Using static methods like System.gc()
14. Reference counting
• Keeps counts for each object
• Count increases as the number of references
increases
• Eligible for gc when count becomes zero.
15. Tracing
• Traverses through the graph
• Starts from the root node
• Marking is done
– By setting flags in object themselves
– By setting flags in separate bitmaps
• Unmarked objects to be unreachable
• Also known as Mark and Sweep alg.
16. Finalize() method
• This method is called before the garbage
collector claims the object
• Available for all the classes in java
• Defined in java.lang.Object
• Purpose of this method is to perform clean up
activity ie, releasing system resources if
held, close the connection if open.
• After executing this method, the program gets
for garbage collection.
17. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
18. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com