• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Grabage Collection Questions

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All.........
I have 3 question :
--------------------------------------------
1)--
In the whizlab simulator: There is question on the garabage collection , says that : any exception occured in the finilize() method .. the garabage collection WILL RUN AND IGNORE IT...I can NOT Understand That..
-------------------------------------------
2)----
If the object is GCed,is the finilze method guaranteed to run..
-------------------------------------------
3)-- How we can re-allocate the objetc in finilaze() method ,so that we prevent the object from GCed....

Thanks
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1)--
In the whizlab simulator: There is question on the garabage collection , says that : any exception occured in the finilize() method .. the garabage collection WILL RUN AND IGNORE IT...I can NOT Understand That..


the garbage collection runs in its own deamon thread, any exception thrown in it cannot propegate through any other threads (e.g. your code). The exception thrown in the finalize() method will be caught straight away and will not blow up the gc (preventing it from continuing to gc other objects) or print stack dumps etc.


2)----
If the object is GCed,is the finilze method guaranteed to run..


yes, part of GCing an object is running its finalize method.


3)-- How we can re-allocate the objetc in finilaze() method ,so that we prevent the object from GCed....


give some other object (that is not currently eligible for GC a reference to the object). For example add it to a collection:

 
Alan Hermin
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.....
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
"give some other object (that is not currently eligible for GC a reference to the object)"
The above statement is wrong. When finalize method of an object executes, the object is eligible for garbage collection i.e. it is not reachable to other objects, hence the best way to resurrect an object in the finalize() method is to assign the 'this' reference of current object to a static reference varaible.
Let me know if you have any doubts about this.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhijit Sontakey:
Hi,
"give some other object (that is not currently eligible for GC a reference to the object)"
The above statement is wrong. When finalize method of an object executes, the object is eligible for garbage collection i.e. it is not reachable to other objects, hence the best way to resurrect an object in the finalize() method is to assign the 'this' reference of current object to a static reference varaible.
Let me know if you have any doubts about this.



Sorry, but I don't think the statement is wrong. When an object is eligible for garbage collection, it means that it is not reachable by any object that is reachable. It is possible for an unreachable object to contain a reference to a reachable object.

Henry
 
Kristian Perkins
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mis-parenthasised the statement, it should be:
"give some other object (that is not currently eligible for GC) a reference to this object"

There is an error in my previous post, but that error is that there is no return type in the finalize method signature. I tested the (modified) code that Abhijit classed as wrong and it ran with output of:

The second line of output "Hi" is the printed toString() method retrieved from the collection showning that the object is still alive after the finalizer is run. The modified code is shown below:


[ April 23, 2006: Message edited by: Kristian Perkins ]
[ April 24, 2006: Message edited by: Kristian Perkins ]
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other situation to worry about is when the GC calls finalize on an object, and the finalize method saves the object by passing a reference to someone else. Now the next time that "saved" object becomes eligible for GC, the finalize method will NOT be invoked automatically.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic