• 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

Garbage Collection Question From JavaCaps Mock.

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I Got the following question from JavaCaps Mock Exam.
12. From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
The given answer is A.
But I think this answer is wrong & answer should be D.Because In the code it'll create two string objects & they'll be placed in the String Pool .So the objects in the string pool will not be (never) garbage collected even if you have asign the reference to the null.
Correct me if I'm Wrong...
Thanks In advance,
Shan.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shan, you are correct. None of the objects are GC'ed since they were created on the string pool. The question is probably assuming strings created with and with out "new" to be the same. if that's the case, the given answer is correct.
Bosun
 
Shan Karawita
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your comment Bosun
Any other comments please .....
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any object shall definitely exist till there is any valid reference to it. But since there will not be any reference to String "Test", after last line - it will be "eligible" for garbage collection.
Keeping Strings in pool helps JVM save on storage - if any new reference crops up to same a String which is ALSO referenced by another variable, then both variables refer to same String object. But I don't think any un-referenced String object can be assumed to be in pool.
Aniruddha
 
Shan Karawita
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aniruddha,
I was also lost in the dark & thought as same as you think before I posted
this post .

What ever the string objects in the string pool will never be garbage collected.I repeat will never be garbage collected
Shan.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic