-4

I have a Java ArrayList with references to other objects stored within the List. If I mark the List as null then when it is garbage collected, will all the stored items in it also get claimed by GC (assuming there are no other references to them)?

thanks, Jakao

Jakao
  • 45
  • 4
  • google first! https://stackoverflow.com/a/38765202/3959856 – Jack Flamp Jun 11 '18 at 08:04
  • When they are collected, really depends on the implementation of the VM – Lino Jun 11 '18 at 08:04
  • 1
    If you still have references to them, they won't be garbage collected. – khelwood Jun 11 '18 at 08:09
  • Don’t “mark the List as null” and stop worrying about the garbage collector. It will do the right thing. And it does it because you should focus on the application logic instead of thinking about the garbage collector’s job. – Holger Jun 12 '18 at 11:55

2 Answers2

0

If you know it (can access it from code / have a reference to it anywhere) it´s there (and won´t ever be collected), if you don´t it might be gone. When is it gone? None of your concern, that´s the point of having a garbage collector.

Max
  • 1,536
  • 1
  • 14
  • 18
0

Assuming there are no other references to them, they will be GC.

In java object reference is an abstract concept, you should not worry about how the JVM manages object storage, but if you interested in memory managment in java, I suggest you deepen the arguments of the weak and soft reference and memory pools.

pixelatedCat
  • 312
  • 5
  • 17