• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

GC Doubt

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
I would say 3 objects that is "Nick" after it was reassigned to Frieds. Then Frieda, after it was reassigned to null and newest name, since reassigning it to name and then to null.
Am i right
 
Trailboss
Posts: 23731
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reality is 0 (zero). All of the string objects listed are in the string pool and can never be garbage collected.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me say something about this type of question. The string objects in the example should be String objects created with the <CODE>new</CODE> keyword. That's because String literals like plain <CODE>String s = "ABC"</CODE> are not treated the same. Their treatment is beyond the scope of the intended question. (But I see that Paul already said so!)
For the purposes of the exam, I am sure that String objects, in a question about GC, will be always '<CODE>new</CODE>' Strings.
Having said that, let's re-write the example:


String name;
String newName = new String("Nick");
newName = new String("Jason");
name = new String("Frieda");
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
I say 1 (b).
 
Venkat01
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
Congrats on becoming the moderator for this group. I have already bookmarked the main discussion page, but how do i know if some of the posts which i have made have a new reply or not. In javacert, since individual messages were there, it was easy to know. Also Marcus's new discussion site has a facility to check only the last day's message. How do we do it here?
Coming back to the question, how can you say only one object will be eligibe for GC (what about the assignment of name to Newestname, i think i am wrong here, but is damm confused). Also if i set Newname and Newestname to null, how many objects will be eligible for GC.
Sorry for the trouble, tony (this is a simple case, but it damm confusing) and thanking you in advance.
 
paul wheaton
Trailboss
Posts: 23731
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you get to the main page for the Saloon, there is link called "Click Here To View Today's Active Topics (all public forums)" Does this do what you want?
Plus, here, when you are registred, the software leaves a cookie on your browser so it can track when you were last here. It will then show you all of the new stuff since you were last here with either lit lightbulbs (the forums page) or with pink folders (a forum page). Not adequate?
As for Tony's new and improved question - his answer is correct.
"what about the assignment of name to Newestname": newestName is a new reference that references the same thing that name does. So there is no new gc opportunity on this line. When name is set to null, newestName is still pointing to the object than name was pointing at, so there is still no opportunity for new gc.
"if i set Newname and Newestname to null, how many objects will be eligible for GC": 3. Or "all of the strings that were created."
 
Venkat01
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
Thanks for the reply. I know about the light bulbs in the top page. What i was referring is that once inside the programmer certification page, how do i know whether any new message have been added or not and is there any way to see only the new messages that have been added (something like marcus's site). That is really good. This is also good, but the only limitation is that i will have to go toall the topics and browse down to see if there is any new message. Even in javacert.com, by just seeing the topic, i was able to decide whether to read it or not, which is not possible here. Please note that this is just a request.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic