• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

garbage collection

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

a) 0
b) 1
c) 2
d) 3
e) 4

according to me answer shoud be b) but this is wrong can naybosy tell what is the correct answer and why?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tring name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

2 objects are eligible for garbage collection. Fromt the last line(name - null), it is obvious(1 object) and then at the point where there is no reference to "Nick" string(newName = "Jason")(2nd object)
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are all String literals ....existing in String pool....

I would go for 0..
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes there are two objects eligible for garbage collection.

Originally posted by Divine Divine:
tring name;
String newName = "Nick"; // variable newName is pointing to "Nick"
newName = "Jason"; // Now newName is pointing to "Jason" but there is
//no ref for "Nick" - 1st Object for GC
name = "Frieda";

String newestName = name;

name = null; // and the second one is name
//Line A

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



name = null; // and the second one is name //Line A



Here the literal referred by name is also referred by newestName

because of the line ...String newestName = name;


Regards
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
Originally posted by Divine Divine:
String name;
String newName = "Nick"; // variable newName is pointing to "Nick"
newName = "Jason"; // Now newName is pointing to "Jason" but there is
//no ref for "Nick" - 1st Object for GC
name = "Frieda";

String newestName = name;

name = null; // and the second one is name
//Line A


--------------------------------------------------------------------------------
The above quote is not correct. name is a refernce and not object.

According to me, it is 1.

1. "nick" String object is refered by newName
2. then, newName reference refers to "Jason". thus no live reference to "nick" object exists. "nick" object elligible for GC.
3. name and newestName reference refers to "Frieda". and in the last line name reference is set to null dat means it does not refer to any object now.

but newestName reference still refers to "Frieda" object. thus it is not eligible for gabage collection.

Please correct me if i am wrong somewhere.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with anju. when reference name is set to null ,object "Freida" can still be accessed by reference newestname and hence is not eligible for garbage collection. only 1 object "Nick" is eligible for garbage collection
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer I think is 0.
The short version:

Unlike most objects, String literals always have a reference to them from the String Literal Pool (which is a collection of references to String objects), even if there's no longer any active thread that has a reference to that String literal.



The long version: Strings, Literally - by Corey McGlone, look under the Garbage Collection subtitle.
[ February 09, 2006: Message edited by: John Brown ]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think its two objects

one is name =null;
and the other one is newname
as here itis reassiging refernce variable
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with John !!!

No objects are eligible for GC.

After read the link he have posted, I definitely understood how Strings are treated behind the scenes.

Very good link, I recommend all to take a look. Thanks John.

In a nutshell Strings are a case apart due to String pool existing in Java.

Basically, all string literals are immutable and created in string pool managed by Java.

So, even you assign a null value to your variable, the string still remains in the pool with a valid reference "pointing" to it.

On the other hand, String objects criated with new are not stored in string pool and the only reference for it is your variable.

The example below was extracted from the link posted by John. Pls take a look :




But if I make a little change in the code above see the results :



Conclusion :

Equivalent String Literals (even those stored in separate classes in separate packages) will refer to the same String object.

In general, String Literals are not eligible for garbage collection. Ever.

Strings created at run-time with new keyword will always be distinct from those created from String Literals.

You can reuse String Literals with run-time Strings by utilizing the intern() method.

The best way to check for String equality is to use the equals() method.

I repeat again. Very nice link. Thanks John.
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A



Bessa and John pointed out correctly. Apart from heap where the runtime objects are being created, there is one string literal pool which will be having the string literals at the compile time!

So in the above code, all the references are to the literals located inthe pool. There is no question of runtime creation of objects here. So the GC will have 0 objects for flushing.

Had the code been like this, then 1 object for GC is appropriate to say

String name;
String newName =new String("Nick");
newName = new String("Jason");
name = new String("Frieda");

String newestName = name;

name = null;

Regards,
Narendranath
[ February 09, 2006: Message edited by: Naren Chivukula ]
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it . Link is really good.
thanks for clearing on this.
 
John Brown
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edisandro and Anju, you're welcome, I merely posted the link Let's thank Corey McGlone, the author of the article. Thanks Corey!
[ February 09, 2006: Message edited by: John Brown ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other thing to mention here is that the topics of String objects and GC won't intersect on the real exam. you'll definitely get questions about how many anything-other-than-String objects are eligible for the GC, and you *might* get questions about how many string objects are *created*, but you won't get how many *String* objects are eligible.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent thread people. Thanks for clarification.

It's nice to know that the exam doesn't muddy the water on this topic by testing garbage collection in the context of String pools. It's kind of like the difference between keywords and reserved words that used to be tested. Thankfully, no longer.

Again, good discussion.

Josh
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic