This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Do waiting threads prevent an object Watch "Do waiting threads prevent an object New topic
Author

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 ?

Thanks
Vipin
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.

Cheers
Vipin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do waiting threads prevent an object's GC ?