File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Do waiting threads prevent an object's GC ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Do waiting threads prevent an object Watch "Do waiting threads prevent an object New topic

Do waiting threads prevent an object's GC ?

Vipin Mohan
Ranch Hand

Joined: Nov 15, 2003
Posts: 79
hi all

If we call wait() method within the finalize()method of an object, will it prevent the garbage collection of that object till notify() is called ?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I imagine it would prevent the collection of any objects, since the finalizer thread will be blocked.

Once, quite early in Java's lifetime, I spent two days trying to understand a deadlock in which one of the two threads involved was the finalizer thread. The subconscious lesson I learned from this was to never do anything non-trivial in finalize() -- this rule has served me well over the years.

[Jess in Action][AskingGoodQuestions]
siva raju
Ranch Hand

Joined: Oct 10, 2003
Posts: 37
Good question dude. As Ernst pointed out, the gc thread will be blocked and no objects marked for finalization in that sweep will be collected until notified. But other gc threads instantiated after this would still run as they are in separate theads.

Siva<br />-----<br />SCWCD 1.4, SCJP 1.4, SCBCD(Preparing), CCNA
Natarajan Shankar
Ranch Hand

Joined: Jun 10, 2004
Posts: 53
Hi Vipin,
As your program is still running GC will not colelct your objects. Thats why, if you see program running 24x7 (batch programs). Initially for few times will be ok then later we will have memory problem.

Best Regards<br />N.Shankar<br />SCJP, SCWCD
Vipin Mohan
Ranch Hand

Joined: Nov 15, 2003
Posts: 79
Thanks guys !
Its clear now.

I agree. Here's the link:
subject: Do waiting threads prevent an object's GC ?
It's not a secret anymore!