Java Garbage Collection Algorithm – Mark and Sweep Algorithm
Stay updated with latest technology trends
Join DataFlair on Telegram!!
In the last tutorial, we studied the garbage collection in Java. Here, we will learn about the different types of Garbage Collection Algorithm in Java: Mark and sweep algorithm. Along with this, we will discuss the phases of this algorithms.
So, let us start the Java Garbage Collection Algorithm.
2. Garbage Collection Algorithm in Java
Java Garbage Collection Algorithms used to remove the unreachable objects, it always runs in the background. There are different types of Garbage Collection Algorithms in Java that run in the background, and among them, one is a Mark and Sweep algorithm.
3. 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 that the space from the memory heap so that it can be used by the programmer again.
The two phases of 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 profundity 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 to a protest and is specifically available by the local variable. We will accept that we have one root as it were.
- We can get to the mark bit for a protest by: markedBit(obj).
Mark Collection Algorithm –
If markedBit(root) = false then
markedBit(root) = true
For each v referenced by a root
b. Sweep Phase
Sweep Garbage Collection Algorithm in Java, as the name proposes it “clears” the inaccessible objects i.e. it clears the store memory for all the inaccessible articles. Each one of those items whose check esteem is set to false clear from the stack memory, 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.
Sweep Collection Algorithm –
For each object p in a heap
If markedBit(p) = true then
markedBit(p) = false
- It is also called tracing garbage collector because it traces the objects.
- Marked bits set as false.
- Reachable objects set as true.
- Non-reachable clear from the heap.
4. Pros & Cons of Mark and Sweep Algorithm
Advantages and Disadvantages of the Mark and Sweep Algorithm:
a. Advantages of 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.
b. Disadvantages of Mark and Sweep Algorithm
- Normal program execution stops while the Java garbage collection algorithm runs.
- It runs different several times on a program.
So, this was all about Java Garbage Collection Algorithm. Hope you like our explanation.
Hence, in this tutorial we learned about the Java garbage collection algorithm, different types of collection algorithms: Java Mark and Sweep algorithm with the pros and cons of Mark and Sweep Algorithm in Java. In the next tutorial, we will learn about the island of isolation.
See also – Java.io.File Class