aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interesting question in GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interesting question in GC" Watch "Interesting question in GC" New topic
Author

Interesting question in GC

Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50

Can anybody predict the output of this program??
I have compiled executed it and found the following output.
Hey, I'm in TestFinalize finalize()
Hey, I'm in TestFinalize finalize()
Just returned from garbage collection
Ans explanation says it won't be same if you run it again.
That seems to be right also. Because we can't predict when the object will be GCed.
Any comments??
I will list the other choices
A: Hey, I'm in TestFinalize finalize()
Just returned from garbage collection
B: Hey, I'm in TestFinalize finalize()
Hey, I'm in TestFinalize finalize()
Just returned from garbage collection
C: Hey, I'm in TestFinalize finalize()
Just returned from garbage collection
Hey, I'm in TestFinalize finalize()

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Mani,
Could you please tell me in which mock exam you found this question.
Thanks.
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
It is from JCertify Mock exam.(trial version)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi mani The other predictions u made about the Answer is correct.Only one thing is Sureout here is The very first statement is printed "hey i am in testfinalize Finalize" ..other order of messages completely depends on the garabage collection runs or Not or successfully completed without any exceptions???
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I wish to ask one thing... slightly off topic from the main point of the discussion...
Is it necessary that the statement
System.out.println("Just returned from garbage collection");
will be printed only after Garbage collection is completed?
Wouldn't the garbage collector run in its own low priority thread in the background even after calling Runtime.gc() ?
Pls clarify someone...
thanks,
Amol
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
Originally posted by Amol S Deshmukh:
Hi,
I wish to ask one thing... slightly off topic from the main point of the discussion...
Is it necessary that the statement
System.out.println("Just returned from garbage collection");
will be printed only after Garbage collection is completed?
Wouldn't the garbage collector run in its own low priority thread in the background even after calling Runtime.gc() ?
Pls clarify someone...
thanks,
Amol

Amol,
I think you are right. Hence, all three are valid answers depending on implementation, unless there was one which said all of the above.
Savithri
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi the statement
System.out.println("Just returned from garbage collection");
is same as the
System.out.println(new String("Just returned from garbage collection"));
When this new String is Created since the reference is not maintained anywhere so it would be eligible for garbage collection only after it is printed.Not before that.[sinc the Object is created only when the statement gets executed !]
Well as a matter of fact garbage collector runs in its own low priority thread

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting question in GC
 
Similar Threads
Question regarding finally block (Devaka, Final Exam, Question 1)
Garbage collection problem
Question about finalize method of GC
Garbage Collection