I am running the following code every two minutes via a Timer:
object = new CustomObject(this);
Potentially, this is a lot of objects being created and a lot of objects being overwritten. Do the overwritten objects get garbage collected, even with a reference to itself being used in the newly created object?
I am using JDK 1.6.0_13.
Thanks for the help.
We’d need to know what was going on inside the constructor to answer the question fully.
But generally, as long as nothing retains a reference to the old object when the new one is created, it will be available for garbage collection, even if the old object is used in the process of creating the new one.
For example:
This won’t retain references to the old
Foowhen the newFoois constructed, so the oldFoocan be GC’d:Whereas if
Foolooked like this instead:…then each newly spawned
Foowould have a reference to the previousFooand none of them would be available for GC.