Garbage Collection in Java (1)
Garbage Collection in Java (1)
1
2
2
3
3
4
4
5
3. Age of an employee.
Also, provide the following methods:
A parameterized constructor to initialize name and age. The ID
should be initialized in this constructor.
A method show() to display ID, name, and age.
A method showNextId() to display the ID of the next employee.
Now any beginner, who doesn’t know Garbage Collector in Java will
code like this:
// Java Program to count number of employees working in a
company
class Employee {
private int ID;
private String name;
private int age;
private static int nextId = 1;
// it is made static because it
// is keep common among all and
// shared by all objects
6
7
X.show();
Y.show();
X.showNextId();
Y.showNextId();
}
// After countering this brace, X and Y
// will be removed.Therefore,
// now it should show nextId as 4.
Id=1
Name=GFG1
Age=56
Id=2
Name=GFG2
Age=45
Id=3
Name=GFG3
Age=25
Next employee id will be=4
7
8
8
9
9
10
// In this case,
// gc will call finalize()
// for 2 times for 2 objects.
} }
public class UseEmployee {
public static void main(String[] args) {
Employee E = new Employee("GFG1", 56);
Employee F = new Employee("GFG2", 45);
Employee G = new Employee("GFG3", 25);
E.show();
F.show();
G.show();
E.showNextId();
F.showNextId();
G.showNextId();
{
// It is sub block to keep
// all those interns.
Employee X = new Employee("GFG4", 23);
Employee Y = new Employee("GFG5", 21);
X.show();
Y.show();
X.showNextId();
Y.showNextId();
10
11
X = Y = null;
System.gc();
System.runFinalization();
}
E.showNextId();
}}
Output:
Id=1
Name=GFG1
Age=56
Id=2
Name=GFG2
Age=45
Id=3
Name=GFG3
Age=25
Next employee id will be=4 Next employee id will be=4
Next employee id will be=4 Id=4
Name=GFG4
Age=23
Id=5
Name=GFG5
Age=21
Next employee id will be=6
11
12
14
15
15
16
live and dead objects – live objects are still in use and dead objects
are no longer needed.
The Garbage Collection in Java operation is based on the premise
that most objects used in the Java code are short-lived and can be
reclaimed shortly after their creation. As a result of garbage
collection in Java, unreferenced objects are automatically removed
from the heap memory, which makes Java memory-efficient.
In general, all Java garbage collectors have two main objectives:
1. Identify all objects that are still in use or “alive.”.
2. Remove all other objects that are considered dead or unused
(i.e., unreachable).
The Java garbage collector performs this task by periodically
identifying and reclaiming memory that is no longer in use. The most
commonly used Java Garbage Collection algorithm is called the mark-
and-sweep algorithm, which follows these steps:
16
17
Marking phase: The garbage collector starts with a root set of
objects (e.g., global variables, stack frames, and CPU registers)
that are known to be in use. It recursively traverses through
these objects, marking each object it encounters as “live” or
reachable. All reachable objects starting from known root
references (such as local variables, static variables, and thread
stacks) are marked as live objects.
Sweeping phase: The garbage collector scans the entire heap,
identifying and reclaiming memory occupied by objects that
were not marked during the marking phase. These objects are
considered garbage. Any objects that have not been marked as
“live” during the mark phase are considered unreachable and
are marked as eligible for garbage collection. The memory
occupied by these unreachable objects is then freed up for
future allocations.
When is an Object eligible for Garbage Collection?
17
18
18
19
19
20
20
21
21
22
22
23
23
24
25
26
26
27
27
28
28