• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interesting question in GC

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is from JCertify Mock exam.(trial version)
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
reply
    Bookmark Topic Watch Topic
  • New Topic