Garbage collection vs manual memory management

Managed memory systems are built on the assumption that you don't want to be tracing memory leak issue in the first place. Instead of making them easier to solve you try to make sure they never happen in the first place.

Java does have a lose term for "Memory Leak" which means any growth in memory which could impact your application, but there is never a point that the managed memory cannot clean up all the memory.

JVM don't use reference counting for a number of reasons

  • it cannot handled circular references as you have observed.
  • it has significant memory and threading overhead to maintain accurately.
  • there are much better, simpler ways of handling such situations for managed memory.

While the JLS doesn't ban the use of reference counts, it is not used in any JVM AFAIK.

Instead Java keeps track of a number of root contexts (e.g. each thread stack) and can trace which objects need to be keeps and which can be discarded based on whether those objects are strongly reachable. It also provides the facility for weak references (which are retained as long as the objects are not cleaned up) and soft references (which are not generally cleaned up but can be at the garbage collectors discretion)


AFAIK, Java GC works by starting from a set of well-defined initial references and computing a transitive closure of objects which can be reached from these references. Anything not reachable is "leaked" and can be GC-ed.