Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one of the mock exams, there is such a question:
If the object has been garbage collected, its finalize() method has been invoked.
The answer to the above statement is false.
However, another mock exam (JQPlus)says:
If the object is being collected by the garbage collector, then its finalize() method is guaranteed to be called.
Is there any difference between those two statements? In case they mean the same, then which is the right statement?
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go for the second statement to be true.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You both are wrong. One golden principle that applies to Java implemented Garbage Collection is that nothing is guaranteed.
Consider that Garbage Collection mechanism runs vide a low priority thread that comes into action when it feels system is relatively idle or tries to initiate when system is running low on recourses. One explicit way to request its initiation is to call System.gc(), but even with this method there are no guarantees. Also finalization MAY occur if initially GC has determined that an Object is no longer accessible. Also a method called System.runFinialization() may be used to REQUEST that the objects marked in the first round of gc be allowed to run their finalize() methods, BUT that may not come true in the case of System.exit() or fatal VM Error.
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I had forgotten about the System.exit() when i said finalize() always executes. In other cases, why do you say that finalize() MAY execute, when they are marked for garbage collection? Because otherwise, i don't see any good use of finalize() unless it fails only in extreme conditions like VM error or exit()
Rgds
Shree
 
ashwini srinivasan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM will perform garbage collection if it needs more memory to continue execution.
You can make a call to garbage collection with System.gc(),but this doesn't guarantee when it will happen.
JVM runs the object's finalize() method prior to garbage collection.In other words, java notifies the object.
The garbage collector does not claim objects in any certain order.
Hope this helps.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the API is quite clear on this matter. see the finalize method in the object class. finalize is always called before an object is destroyed(garbage collected).
the answer from the first test was dead wrong.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well shree consider the following;
an object is no longer accessable and is marked for garbage collection. Now when gc runs it arbitrarly may allow finalize method of any of its choice of objects or of all objects, depending on circumstances, to be executed. Once finalize methods of any objects have been executed and the second the, from the prespective of finalized objects, of GC (whenever that happens) proves that the objects(finalized) are no longer accessable the objects will be collected.
Now if the objects are finalized and GC is running the JVM implementation does say that the objects will be collected.
But the first two stages are not guranteed, thus there is no sure way to say that a particular no longer accessable object will be given a sure clean shot to invoke its ptotected void finalize() throws Throwable method. Thats even why it is suggested that thoes classes that also access resources other then the memory should not rely on finalize method.
Why it is such a unpredictable business, well you might know that GC concept is there since 1965's SmallTalk, Eiffel, etc all had it, but all implemented diffrent implementations, some guranteed and some with partial gurantees for resurrection of resources its just that Gosling and Company chosed a restrained stratergy, probaly because Java (Oak/Green/Star 7) was meant to survive as a platform with limited resources.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I originally posted the question. I read all the discussion on the subject. However, I still don't know which one is the right answer.
 
Adeeshwar Singhal
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I agree with u JaiPei that we r unable to get the right answer.
I think that answer should have been true. But as u said it is false, we need to wait till an expert comes with his conclusion.
Adeesh
( Anybody interested in Question on Overriding, click on the Tricky one..........again by me. )
----------------------------------------------------
Get a Brigade Ranchians, Ranch has caught fire.................
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the object has been garbage collected, its finalize() method has been invoked?
If you chaps had read the explaination above you would have realized that the mechanism is such that the finalize() method will have definately been invoked if the Object HAS BEEN collected. So the Answere the question is TRUE.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"But the first two stages are not guranteed, thus there is no sure way to say that a particular no longer accessable object will be given a sure clean shot to invoke its ptotected void finalize() throws Throwable method. Thats even why it is suggested that thoes classes that also access resources other then the memory should not rely on finalize method."
so it might become accessible again, so it's finalize method might not be called? why is that a problem? It will become inaccessible again sometime and finalize() will eventually get called no?
as i understand
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes in a way.
Consider that if a object is accessable through out the course of an application, that can happen, the JVM when exits does clean up but will not definitely let all pending finalization happen. Its like when JVM goes it goes in a hurry. So the Java team in JDK 1.2 gave us another mechanism to play with "ShutdownHooks", in contrast to finalize methods they are guranteed to be executed on JVM exit.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont know about "shutdown hooks"(or maybe i do) but i do know we arent supposed to count on finalze.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Randall cut the maybe crap, we all know you are a smart guy underneath that wolfish grin of yours, trying to protray like us, the poor no good kids.
Come out of that shell help us buddy.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amond>i think i can pass but i dont think i can score well yet. theres still a lot i'm unsure of. the worst part for me is details about methods of class x.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer given in JQPlus is correct. In fact, this is the only thing guaranteed by the GC mechanism. If the GC thread has decided to destroy an object then just before destroying the object, it will definitely call the object's finalize method.
Similarly, the statement: If the object has been garbage collected, its finalize() method has been invoked.
is TRUE.
Although this guarantee is not very useful because whether or when the object will really be destroyed is not guaranteed! That's why is is advised not to rely on finalize() method.
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


[This message has been edited by Paul Anil (edited November 30, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic