Java Garbage Collection Algorithm – Mark and Sweep Algorithm

Free Java courses with 37 real-time projects - Learn Java

1. Objective

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.

Java Garbage Collection Algorithm

Java Garbage Collection Algorithm – Mark and Sweep 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.

Do you know What is Interface in Java?

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).

Related Topics – Decision Making in Java ProgrammingJava 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 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.

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 tracing garbage collector because it traces the objects.

Example –

  1. Marked bits set as false.
  1. Reachable objects set as true.
  1. Non-reachable clear from the heap.

4. Pros & Cons of Mark and Sweep Algorithm

Advantages and Disadvantages of the Mark and Sweep Algorithm:

Java Garbage Collection Algorithm - Mark and Sweep Algorithm

Advantages & Disadvantages of 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.

Read Difference between Abstraction & Encapsulation in Java

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.

Quiz on Java Garbage Collection Algorithm

5. Conclusion

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

For reference

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

No Responses

  1. Nitish says:

    is it done in stack memory ? i doubt

Leave a Reply

Your email address will not be published. Required fields are marked *