0

I know that when an object in memory in not referenced by any other object it is a candidate for garbage collection in java. But what if there are a bunch of objects referencing each other but are inactive...would the memory of these objects be cleared by the garbage collector??

1 Answers1

1

Java Virtual Machines don't use reference counting. They use Mark-Sweep and copying algorithms for garbage collection which guarantee the removal of any objects that can no longer be reachable through object references, even cycles.

Objects that are inactive, or unused, but still are reachable through object references are never garbage collected. They won't be cleared. Memory leaks in Java are caused by such objects, like unused event handlers, collections, etc.

You can read more about that here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

helderdarocha
  • 23,209
  • 4
  • 50
  • 65