Exlude a reference from blocking garbage collection of an object in Java

I have a List in Java which allows to use the same object at multiple places. (Table rows, so they are not cached multiple times in memory).

Is there a way to tell the garbage collector, that he is allowed to remove object that are only referenced by this List, and nowhere else?

Or is there a better design for sharing objects like this?

Jon Skeet
people
quotationmark

Is there a way to tell the garbage collector, that he is allowed to remove object that are only referenced by this List, and nowhere else?

Sort of. You want to make it a List<WeakReference<T>> or something similar. See the documentation for WeakReference and SoftReference for details about the degree to which each of them inhibits collection.

Now when you use the elements of that list, you'll need to check whether the value within the WeakReference or SoftReference has already been collected, so it's not a transparent change - but I think it's what you're looking for.

people

See more on this question at Stackoverflow