Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can unreferenced objects still run if the garbage collector hasn't deleted them?

If an object is waiting to be garbage collected, but contains a thread that was running when the last reference to the object was changed, will that thread still run and will the code still execute? So could you potentially have a bunch of ghost objects that should be deleted, but are having an effect on your code? How do you prevent this? Is there a way for an object to tell if it is waiting to be collected?

like image 774
sinθ Avatar asked Dec 13 '25 08:12

sinθ


1 Answers

A thread wont be garbage collected until it has finished, so if your threads never finish, they will not be available for garbage collection. As noted by fge in the comments, often times the thread will have reference to the enclosing object, which will of course prevent the thread from being garbage collected.

As for knowing if an object is on its way to be collected, this answer has some nice details on what you can(and can't) do.

Other than that, you can suggest the jvm to run garbage collection with system.gc(), but there is no guarantee the jvm will run the garbage collector.

like image 97
Zavior Avatar answered Dec 16 '25 22:12

Zavior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!