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??
Asked
Active
Viewed 138 times
0
-
1see http://stackoverflow.com/questions/1910194/garbage-collection-in-java-and-circular-references for an explination – Jon Taylor Feb 23 '14 at 23:57
-
2All object which are not strongly reachable are candidates, no matter what they reference. – Peter Lawrey Feb 24 '14 at 00:02
1 Answers
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