Java Garbage Collection Algorithm – Mark and Sweep Algorithm
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
In the last tutorial, we studied garbage collection in Java. Here, we will learn about the different types of garbage collection algorithms in Java: the Mark and Sweep algorithm. Along with this, we will discuss the phases of this algorithm.
So, let us start the Java Garbage Collection Algorithm.
Garbage Collection Algorithm in Java
Java Garbage Collection Algorithms are used to remove the unreachable objects, it always run in the background. There are different types of Garbage Collection Algorithms in Java that run in the background, and among them, one is the Mark and Sweep algorithm.
Do you know What is Interface in Java?
Mark and Sweep Algorithm
An algorithm for Java Garbage Collection should basically perform two functions. Firstly, it should locate and detect unreachable objects, and secondly, it should free the space from the memory heap so that it can be used by the programmer again.
The two phases of the Java Garbage Collection Algorithm are –
a. Mark Phase
Mark Garbage Collection Algorithm in Java, at the point when a question is made, its check bit is set to 0(false). In the Mark stage, we set the marked bit for all the reachable items (or the objects which a client can allude to) to 1(true). Presently, to play out this task, we just need to complete a diagram traversal; a depth-first look approach would work for us. Here we can consider each question as a hub, and after that, every one of the hubs (protests) that are reachable from this hub (protest) is gone too, and it goes ahead till we have gone by all the reachable hubs.
- Root is a variable that accesses a protest and is specifically available as a local variable. We will accept that we have one root as it is.
- We can get to the mark bit for a protest by: markedBit(obj).
Related Topics – Decision Making in Java Programming & Java Exception Handling
Mark Collection Algorithm –
Mark(root)
If markedBit(root) = false then
markedBit(root) = true
For each v referenced by a root
Mark(v)
b. Sweep Phase
Sweep Garbage Collection Algorithm in Java, as the name suggests, “clears” the inaccessible objects, i.e., it clears the stored memory for all the inaccessible objects. Each one of those items whose check esteem is set to false is cleared from the stack memory, and for every single other question (reachable articles), the stamped bit is set to false.
Presently, the check an incentive for all the reachable articles is set to false since we will run the calculation (if required), and again we will experience the stamping stage to check all the reachable items.
Must Read Method Overloading vs Overriding in Java in detail
Sweep Collection Algorithm –
Sweep()
For each object p in a heap
If markedBit(p) = true then
markedBit(p) = false
else
heap.release(p)
It is also called a tracing garbage collector because it traces the objects.
Example –
1. Marked bits set as false.
2. Reachable objects set as true.
3. Non-reachable is clear from the heap.
Pros & Cons of Mark and Sweep Algorithm
Advantages and Disadvantages of the Mark and Sweep Algorithm:
Advantages of the Mark and Sweep Algorithm
- It is a cyclic process, i.e., it is an infinite loop.
- No additional overheads occur during the execution of an algorithm.
- This algorithm works when the space left in the memory is quite low.
- The Mark and Sweep algorithm works only when the garbage collector is active, so that it does not disturb the execution of a program.
- It searches for every object that is not in use. Hence, removing the unused element from the depth.
Read the Difference between Abstraction & Encapsulation in Java
Disadvantages of the Mark and Sweep Algorithm
- Normal program execution stops while the Java garbage collection algorithm runs. Causing a hindrance in running the application.
- It runs several times differently on a program.
- The smaller free space is not consumed properly and remains unused, resulting in a loss of memory.
So, this was all about the Java Garbage Collection Algorithm. Hope you like our explanation.
Conclusion
Hence, in this tutorial, we learned about the Java garbage collection algorithm, different types of collection algorithms: the Java Mark and Sweep algorithm, and the pros and cons of the Mark and Sweep Algorithm in Java. In the next tutorial, we will learn about the island of isolation.
See also – Java.io.File Class
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google




is it done in stack memory ? i doubt