2

I'm currently writing a java socket server and i was wondering what will happen in the next situation:

mainClass create Lobby class and add it to an array in the mainClass.

the Lobby class create a Room class and add it to an array in the Lobby class.

the Room class hold a reference to the Lobby class.

the mainClass remove the Lobby instance from the array

the lobby hold reference to the room and the room hold reference to the lobby, will they ever be cleaned up by the gc?

Omore
  • 614
  • 6
  • 18
dvrer
  • 589
  • 6
  • 17
  • The GC should be called the *root reference-able objects retainer.* If an object doesn't have strong references from the GC roots, it can be cleaned up regardless of ciruclar reference. – Peter Lawrey Apr 13 '17 at 12:03
  • Modern garbage collection is much more complicated than simple reference counting. Basically the heap is searched for reachable, "alive" objects; and anything else is garbage. Hint: such things are documented a zillion times ;-) – GhostCat Apr 13 '17 at 12:04

1 Answers1

0

If your objects don't have some references from GC Roots, they will be deleted.