Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how Garbage collector works under the hood to collect dead object?

I was reading up on garbage collection. As we know, Garbage collection collects dead object and reclaims memory. My question is, how Collector comes to know that any object is dead? what data-structure it use to keep track of live objects?

I was researching about this, I found, GC actually keep track of live objects, and marked them. every object that is not marked consider as dead. I guess, there is some thing called object-tree is used for this purpose. but how its works I don't know.

Here are my list of questions.

  • what is object-tree?
  • how its works? when we create any object with new keyword, how it affects that object-tree?
  • who maintains this tree? jvm, GC, or that's how objects get stored in heap?
like image 542
Muneeb Nasir Avatar asked Oct 17 '25 07:10

Muneeb Nasir


1 Answers

The tree is just a conceptual view on the memory heap and the references between objects. It does not exist as a separate entity.

And it's not really a tree since it can contain cycles, so calling it a graph would be more accurate. During garbage-collection it is visited in a tree-like manner (breadth-first or depth-first traversal) by following references and skipping those already visited.

http://en.wikipedia.org/wiki/Tracing_garbage_collection

like image 153
the8472 Avatar answered Oct 19 '25 22:10

the8472



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!