2

A quick question about the Java Runtime Garbage Collector.

Take this scenario:

Two Objects both hold a reference to the other. No other Objects hold any reference to these Objects. The Objects are not doing anything - they're not Runnables, if they are their threads have ended.

Would the Garbage Collector collect these objects, because I'm using these Objects in massive amounts and 'disposing' of them by cutting references... but are they really being disposed?

1 Answers1

1

Yes, the elements are eligible for collection. See:

What are the roots?

Whether they will be collected or not is up to the garbage collector.

Community
  • 1
  • 1
ccleve
  • 15,239
  • 27
  • 91
  • 157
  • Ah, so if I can't access them, they'll be collected? Or at least that's the idea I get from that page? –  Jan 07 '13 at 23:05
  • Yes, but keep in mind that the garbage collector operates on its own schedule. Objects can hang around for a long time. The only guarantee is that they'll be collected before you get an out of memory error. – ccleve Jan 07 '13 at 23:11