---
title: "Comment by Shubham Kesri on \"06 Java - Memory Management and Garbage Collector\" | Hashnode"
description: "In Mark and Sweep, the referenced objects are marked and unreferenced objects are swept from heap. Working of Grabage Collector say otherwise."
url: "https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818"
---

[Discussion](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f)

Comment [06 Java - Memory Management and Garbage Collector](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f)

[Shubham Kesri](https://hashnode.com/@kesri)

[Oct 21, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818)

In Mark and Sweep, the referenced objects are marked and unreferenced objects are swept from heap.

**Working of Grabage Collector** say otherwise.

[Chetan Datta](https://hashnode.com/@chetan77)

[Oct 22, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-6717419d64973d9a019274d5)

Can you please provide more details or quote the exact places where the referenced objects are marked and swept?

(Working of Garbage Collector) The garbage collector's operation correctly uses the mark and sweep algorithm. That's the right one.

[Shubham Kesri](https://hashnode.com/@kesri)

[Oct 22, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-6717ca7bdc832f3a26747bf4)

Please check this:- [geeksforgeeks.org/mark-and-sweep-garbage-collecti…](https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/) [Chetan Datta](https://hashnode.com/@chetan77)

[Chetan Datta](https://hashnode.com/@chetan77)

[Oct 27, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-671db046c19bada9996b6c78)

No, what I mean is to provide the exact location in my article where the referenced objects are marked and swept. Just copy that line and paste it here, and I will correct those places.

Sorry for the delayed response :)

[Shubham Kesri](https://hashnode.com/@kesri)

[Oct 27, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-671dc2af3e4f29a628f38536)

**Garbage Collector**/**Working of Garbage Collector**/1st Run of GC

Objects o2 and o5 do not have any references. So mark these two.

[Chetan Datta](https://hashnode.com/@chetan77)

[Chetan Datta](https://hashnode.com/@chetan77)

[Oct 27, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-671dc5f6617caad6423be4f2)

[Shubham Kesri](https://hashnode.com/@kesri)

"Objects o2 and o5 do not have any references. So mark these two."

this is correct one

At a given timestamp, the created objects o2 and o5 are present, but no variable is pointing to these objects. Hence, the garbage collector marks these two objects and sweeps them away.

[Shubham Kesri](https://hashnode.com/@kesri)

[Oct 27, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-671dd134448513664b1e813a)

[geeksforgeeks.org/mark-and-sweep-garbage-collecti…](https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/)

this one clearly states that, "In the Mark phase, we set the marked bit for all the reachable objects (or the objects which a user can refer to) to 1(true)."

object that are reachable (i.e., referenced one) are marked and those who are not reachable from how could they be marked ??

In this context, marking mean making sure the object is reachable i.e, still referenced in the program.

In the **Garbage Collector/Working of Garbage Collector/1st Run of GC**/Sweep

I think it should be "Keep the marked objects and sweep the rest to the appropriate survivor space".

Please share the reference/links you referred to write these, maybe that could help in getting on the same page.

[Chetan Datta](https://hashnode.com/@chetan77)

[Chetan Datta](https://hashnode.com/@chetan77)

[Oct 27, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-671e4f160d5009e184cf3452)

Now I understand your point [Shubham Kesri](https://hashnode.com/@kesri)

Please let me know if there are any additional changes required in the updated version.

The corrected (updated) Mark and Sweep algorithm is as follows:

First, we delete unreferenced objects that are no longer in use. Mark the surviving referenced objects. Sweep the marked objects to the next survivor level.

I will update the main article shortly. Thank you for taking the time to help me correct the logic!

[Shubham Kesri](https://hashnode.com/@kesri)

[Oct 29, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-6721209a16b70793e8c0476a)

Hi [Chetan Datta](https://hashnode.com/@chetan77), thanks for effort and commitment,

I would frame it in this manner,

"Firstly, we mark the referenced objects in the Mark phase. followed by shifting/copying of the marked objects to appropriate survivor space and Then, the unmarked objects are removed to free up the memory."

For the Mark, Copy & Sweep occurrence sequence, refer - **Serial Garbage Collector** in [codecurated.com/blog/understanding-how-java-garba…](https://codecurated.com/blog/understanding-how-java-garbage-collector-works/)

Thanks

[Chetan Datta](https://hashnode.com/@chetan77)

[Nov 2, 2024](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-6725cd4262b4b21d3db24f61)

Hi [Shubham Kesri](https://hashnode.com/@kesri),

Noted! I'll make the necessary changes and link the references you've quoted here.

Thank you for sharing the resources.

[Shubham Kesri](https://hashnode.com/@kesri)

[Feb 26, 2025](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-67bf1e2d6e2d865791d06582)

Hi [Chetan Datta](https://hashnode.com/@chetan77), any updates ?

[Chetan Datta](https://hashnode.com/@chetan77)

[Mar 16, 2025](https://hashnode.com/posts/06-java-memory-management-and-garbage-collector/6619025b7e2645a9e7554e1f/comment/6715fe86956a891637ec4818#reply-67d64369c426b810f91bc07d)

Updated [Shubham Kesri](https://hashnode.com/@kesri)
