Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interesting question in GC

 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani,
Could you please tell me in which mock exam you found this question.
Thanks.
 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is from JCertify Mock exam.(trial version)
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic