• 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

Question on GC

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question is from a mock exam.
1.String string1 = "Test";
2.String string2 = "Today";
3.string1 = null;
4.string1 = string2;
How many objects will be garbage collected after Line//4.
The answer given is 1
But i think the answer should be 0
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess some of the mock exams consider String literals to be String objects and that is why we see this kind of questions coming up. I can only hope that SCJP doesn't do it.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, i do hope so..
Anyway can you suggest me some good link along with examples
on GC.. I feel i am still weak at this subject.
thanks
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends, whether it is on heap or string pool, it still occupies memory correct, which needs to reclaimed, so what is wrong in reclaiming those unused memories of string pool. otherwise string pool will go out of memory after some time.
i welcome explanations which can through further light on the topic.
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G there is nothing wrong in memory reclamation, just that String objects created in the pool are not GC'ed but the objects created on the heap are. Pick up a bone with Sun people if you have any problems with that. I a'int getting into that fight.
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The wording was bad for me. At first I thought it should be two because AFTER line 4 string1 and string2 will "disppear" like local variables should. Anyways, hopefully the exam will be more clear.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember from Sun's site that the exam will consider String pools non-existent, and every String is a String object.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic