• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

GC Question in Mind Q

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mind Q #36
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A

My answer is 0. The right answer is 1. Is the object "Nick" is eligible for garbage collection?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the object name is available for GC as it has been assigned to null.
Correct me if i am wrong.
Regards,
Sharmila
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, in my opinion, i think "Nick" is not referenced by any variable any more so it's eligible for the garbage collector.
Regards,
James
 
Sharmila Abkari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
But i don't understand "Nick" is not even an object. I think Name is the object, which is no longer referenced.
Please clarify.
Rgds
Sharmila
 
Sharmila Abkari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I get it now, reference's are not GC only Objects are.
So but i still have a doubt, if nick is available for garbage collection, then why is not the object referenced by name, which is "Frieda".
Please someone tell me.
Rgds
Sharmila
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the object referenced by name is not eligible for garbage collection as it is still referenced by newestname
as for the object which contains nick
this object was referenced by newname and the newname was changed to jason
so nick object is no more referenced hence garbage
but my doubt here is when we say strings are immutable how is it that the data contained in newname is changed from nick to jason

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kenneth,
u are correct. none of the objects will be eligible 4 gc because they have been created on the pool & not by new. see RHE 4 details.
Regards,
kaushik
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaushik,
Indeed the "Nick" string object in the pool will be garbage collected. Let me explain the flow at each point:

Thus we see that ONLY "Nick" is eligible for garbage collection.
Hope this suffices the discusssion here.
Rgds,
Ravindra Mohan.
 
Kenneth Ho
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm very clear now. Thx all of u.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sona,
u r little confused abt the immutability of string. strings r immutable but not reference to strings.
so
//Jack is allocated memory. a is just a reference to it.
String a = "Jack";
//can change a to point to anything but doesnt mean that i am
//playing with Jack. Jack is there in its memory area and noone
//can do anything to it. creating Jim in a new memory area and
//a is pointing to it now
a="Jim";

hope this funda is clr for u now,
Vivek
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kushik,is right, string literal's are not garbage collected it is string object which get's collected.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I think NITIN AND KOUSHIK both r right.
The String created in the pool r not eligible for GC.
If poll's Strings r eligible for GC then wht. will be the diff. btw. String pool and heap.
So..I think String created in the pool r not eligible for GC even u explicitly give a String pool's object null value ...
thanks.
Ratul Banerjee
 
kaushik banerjee
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravindra,
i think u need 2 work on u'r concepts of gc,object creation on pool & heap. i suggest u go through RHE as this portion has been dealt very well. u can refer 2 previous discussions in this site where big boss 'AJITH' has explained gc phenomenon very clearly.
Regards,
kaushik
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaushik,
Thanks for the suggestion .
Well, my reply to your suggestion is that string literals can't hang in space without any reference to it. If the string literals are NOT GC any time then Java's JVM is in trouble because in that how will JVM relinquish resources that local string variables hold. Please refer to Khalid Mughal's Mock test Q18 at pp 705 and answer the at pp 723. In case you dont have the book here is the question :


Q18 Which is the earliest line in the following code after which the object created on line marked (0) will be a candidate for being garbage collected, assuming no compiler optimization are done ?
<code>
public class Q769a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)

}
}
</code>
Select the one right answer.
<code>
(a) The line marked (1)
(b) The line marked (2)
(c) The line marked (3)
(d) The line marked (4)
(e) The line marked (5)
</code>
Answer provided at pp 723 is :
<code>
Q 18 (c)
At (1), a new string is constructed using the "bue" string, but
no additional reference to "bye" string object are created. On the line below. an additional refernce to "bye" is created. At line (2) , the original refernce to "bye" in b is overwritten,
but d still contains refernce to "bye". At line (3), the reference d also stops denoting "bye". Now the "bye"
string object cannot be referenced through any reference, and thus is a candidate for garbage collecting.
</code>


That took a lot of my time typing. I GUESS THE CONFUSION IS
CLEARED NOW. AND PLEASE AJITH IS NO GOD THAT HE CANT GO WRONG.
Cheers,
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 05, 2001).]
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ravindra....,
For a string literals, When the JVM is shutting down, It will simply returns all its memory, it won't be bother for running GC.
So according to your quote, I must say : If the string literals are NOT GC any time then Java's JVM WILL NOT having any trouble.
I must agree with Khausik, pool..heap..pool..heap..pool..heap !!
stevie
 
kaushik banerjee
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravindra,
i fully agree with u that Ajith is no GOD but doesn't the same fact applied for Khalid & Mughal. if u have gone through the latter u will see they have carefully avoided the topic of string gc(object creation on heap & pool) whereas RHE has dealt with this very topic explicitly(Mind u Simon Roberts is one of the exam designers---yes he too is not GOD but!!!). Thx 4 taking the trouble of writing so much.

Thanks a lot Steve . like in the past u have been of real help to me
Regards & best of luck . Happy studying,
Kaushik
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
I enjoyed this long and contraversial discussion we had in this
thread, and I there was another thread that was discussing this
hotly debated issue at http://www.javaranch.com/ubb/Forum24/HTML/009611.html The conclusion was drawn by the intervension of Jane Griscti (Bartender) of this forum. It was agreed that as far mock questions goes MY ANSWER IS INDEED CORRECT but in ACTUAL SCJP
EXAM we will not get question pertaining to Garbage collection based on String Literal BUT OBJECTS.
Cheers ,
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 06, 2001).]
 
kaushik banerjee
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravindra,
Lately ew haven't been seeing eye 2 eye on a lot of things but i am sure u will agree that our 2 basic objectives of visiting javaranch are laerning JAVA & clearing SCJP with as high a score as possible & not 2 score full marks in mocks. Thx 4 the link to Jane's post but pls when posting or quoting, refer to the entire thing not half extracted ones which support u'r viewpoint. In fact Jane in his 1st line says:-
1)The JVM specifications do not say how the gc is to be implemented; it is only required to work on objects created in heap memory. String literals are stored in a constant_pool, not in the heap. See JVM �3.5.3, �2.17.7 and �5.1.
2)Most of the mock exams ignore this and use String literal objects in the gc examples.
u seem 2 have ignored them. the question here is not 2 win an argument or 2 but as i stated to learn JAVA & do well in SCJP. it is of no use when in trying to win a petty argument u try to muddle other's concepts.Applying u'r concept even the "mock" makers are not god .so give them a break & don't take their every question in toto.Do let me know what u feel
Regards & best of luck
Kaushik banerjee
 
reply
    Bookmark Topic Watch Topic
  • New Topic