3

I understand that when an object is added to a List, the List retains a reference to that object based on answer from this question Is this java Object eligible for garbage collection in List

How then how do you make the object in the List eligible for garbage collection so that it's removed from the heap and not taking up memory?

I ask because in JavaFX, a Vboxs getChildren method returns observable list containing the children nodes of the vbox. If a UI element is removed but not eligible for garbage collection, will this object still be on the heap consuming memory?

Community
  • 1
  • 1
user2312688
  • 521
  • 7
  • 17
  • 1
    An item only becomes eligible for GC when no other item references it. Therefore, if it is in the List it cannot be GC'ed. Only when it is removed from the list will it be eligible – jr593 Aug 04 '16 at 10:44
  • What do you mean by "If a UI element is removed but still not eligible for garbage collection?" The only way that happens is if there are other references to the element somewhere. Once you remove it from the list, the list has no references to it, so if you are not keeping other references to it, it will be eligible for garbage collection. – James_D Aug 04 '16 at 11:01

3 Answers3

2

Removing the references from that should make them subject of garbage collection (as long as no other object keeps references!).

You know, that is the whole idea how a GC works: it keeps those objects that are alive (can be reached from your initial starting point). Everything else is garbage; and subject to disposal once the GC decides to collect that garbage. And just to be precise here: you have to understand that these are two different activities. There might be a long time between object X "turns into garbage"; and "X gets collected; and memory is freed up".

One can use WeakReferences to avoid this; but of course, that requires that some piece of code would be pushing such WeakReference objects into the list initially. So, if you "own" this code, you could change that. But of course: that means that you always have to check if the object behind the WeakReference is still there when accessing the WeakReference.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

How then how do you make the object in the List eligible for garbage collection so that it's removed from the heap and not taking up memory?

Assuming that those objects are only referenced by this List, simply use the clear method

If a UI element is removed but not eligible for garbage collection, will this object still be on the heap consuming memory?

As long as an object is been hard referenced by at least one another object that is not itself eligible for garbage collection, the objet will be itself not eligible for garbage collection so it won't be collected by the GC it will then remain in the heap.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

If you cannot remove the object from the List, the only way I can think about to handle this would be wrapping your Objects into a WeakReference.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Not possible for children of a `VBox` since the type parameter of the list is `Node` not `WeakReference`. – fabian Aug 04 '16 at 10:47