Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Question on a Mack question on GC

 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
static boolean status;
public static void Process(boolean status) {
if (!status) {
String s = "Java";
int i;
s = "JavaScript";
String x = "Java";
String y =x;
x=null;
i =1;
};
}
How many objects are eligible for Garbage Collection?
1)cannot determine.
2)1
3)2
4)3
5)compile-time error occurs.
6)No object is eligible for GC.

Answer: 2
Why?? String s = "Java" and String x ="Java" is the same thing. I thought the answer was 6. since y is holding "Java"
, s is holding "JavaScript" and i=1.

thanks... 1 more day till the exam!!
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fei,
The answer 2 is correct!
If you go through the code carefully, you will realize, that when statment s="JavaScript" is executed, "Java" object gets unreferenced, hence it becomes eligible for Garbage Collection.
Now when statement String x = "Java" is executed a new object/string literal "Java" is created.The next statement String y = x makes the y and x refer to the literal "Java".The statement that follows, x = null, removes the x reference to "Java" thereby making it eligible for Garbage Collection.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you find this code? I think we won't have any question in the exam regarding String literals reachabilty. Because String literals are interned in a pool of String Objects, and we are not required to know the mechanism by which these interned literals are made eligable for recolection. I particularly think that they are not.
int i is not an object so it is irrelevant here.
This code shows that the object pointed by "s" and "u" are not garbage collected. Because they are String literals. But t is not, so it is recolected.

output:
Java
true
null
Java3
The right answer, if considereing the particularity of String literals, is thefore 6.

hope helps.
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You think it is answer 6)
Me TOO!!!
who is right here??
i got from Jtips mock exams
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the String literal can be collected as well as other objects, i think its 2). only the "java" will be collected at the line "s="javascript";". but the question is rather bad. when the control goes out of the method, all the string object will be collected.
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Desai Sandeep:

The next statement String y = x makes the y and x refer to the literal "Java".

The statement that follows, x = null, removes the x reference to "Java" thereby making it eligible for Garbage Collection.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)


Hi Sandeep
garbage collector collects object, not references. And x is reference hence question does not arise of 'x' to be collected by gc.
correct me if I am wrong
------------------
Regards
Ravish
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java3 shows that String literals are not garbage collected.
Java2 is not a literal so it's garbage collected.
Java is not garbage collected because is a String literal. Thus when a new Java literal is created, x is pointing the same String object that s was. This is why true is printed in the example.
Not all the objects created in a method, that doesn't "save" these objects, are made eligable for recolecting when the method returns. Because the Strings in the pool are still referenced by the pool they are not made eligable for recolecting when the method returns.
This can be shown placing these sentences at the end of process:
System.out.println("Java hashCode= " + ws.get().hashCode());
System.out.println("Java3 hashCode= " + wu.get().hashCode());
and these ones in main after the call to process:
System.out.println("Java".hashCode());
System.out.println("Java3".hashCode());
they will print the same hashCode for both "Java" and "Java3", as the ones that these literals had in the process method. This shows that the String objects representing them weren't destroyed after the returning of process, because they were created in the pool.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic