*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on GC" Watch "Question on GC" New topic
Author

Question on GC

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
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.


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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
g krishnan
Greenhorn

Joined: Mar 30, 2001
Posts: 23
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

Joined: Apr 17, 2001
Posts: 74
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.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
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

Joined: Apr 06, 2001
Posts: 371
I remember from Sun's site that the exam will consider String pools non-existent, and every String is a String object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on GC
 
Similar Threads
Garbage Collection of Strings
garbage collection ?
Ambiguous GC?
how many objects are eligible for garbage collection?
how many objects are eligible for garbage collection?