• 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

gc ??

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question at MindQ
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
any comments/answers???.
--Farooq
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My comment is that questions about GC should NEVER use String as an example due to the complication introduced by the String constant pool. In this code no new String objects have been created so there are none that are eligible. "Nick" etc are all constants created when the class loaded.
Bill
------------------
author of:
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Muhammad,
Bill is correct; string literals are not gc'd however many of the mocks use them in gc questions.
Here's an earlier thread on the same question that explains the question.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill and Jane,
I appreciate your help, I found this question on MindQ and the answer there was one object.
--Farooq
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • 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 gc after the last line??
String a = new String ("Hello");
String b = new String ("World");
String c = a + b;
a = null;
b = c;
c = null;
b = c;

AAA
--Farooq
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it should be 2 Farooq!
------------------
azaman
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think two objects are eligible for GC.
Pls. correct if wrong ..

How many objects are eligible for gc after the last line??
String a = new String ("Hello");
String b = new String ("World");
String c = a + b; // c contains "HelloWorld"
a = null; // as a has no more reference, eligible for GC
b = c; // b contains "HelloWorld"
c = null; // c has no reference, but b still contains "HelloWorld"
b = c; // b now contains null reference , so it is eligible
for GC

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what MindQ TBTs say about String literals


String literals, which are enclosed in double quotation marks, create an instance of a String class object whose contents cannot be changed.


is it correct? I know, there had been lot of dicussion on this topic..but still i seems to bring confusion with every new post..
Thanx
 
Oh the stink of it! Smell my tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic