Forums Register Login

Interesting question in GC

+Pie Number of slices to send: Send

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()

+Pie Number of slices to send: Send
Mani,
Could you please tell me in which mock exam you found this question.
Thanks.
+Pie Number of slices to send: Send
It is from JCertify Mock exam.(trial version)
+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 780 times.
Similar Threads
Garbage collection problem
Question regarding finally block (Devaka, Final Exam, Question 1)
Garbage Collection
Question about finalize method of GC
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:59:52.