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

garbage collection

 
Ranch Hand
Posts: 290
  • 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;
};
}

public static void main(String [] args) {
Test t = new Test();
Process(status);
}
}

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.

Hello jane gristi,
I have read your view's on the garbage collection in one of the previous post.I have taken a question related to garbage collection from jtips.net site.
In one of your previous post you mentioned that only the object's created on the heap are eligible for the garbage collection.In my opinion either 1 or 6 is correct,the given answer is 2 ,Please throw some light on it.

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL
nitin, you said the answer is 2, do you mean option 2 from the list or is it that 2 objects are available for garbage
collection ??
Please make it clear.
Thanks in advance.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am talking about the second option.The answer given in the mocktest is 1.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the nswer given in the mock test is
2)1.I am sorry for the confusing stuff.
 
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 nitin,
Technically, no object would be available for garbage collection (ans 6). Two objects are created in the String pool, "Java" and "JavaScript". These would not be garbage collected unless the system they running on had optimized the JVM to run the garbage collector on the String pool.
A third object 't' is created in <code>main()</code> however it's reference is never set to 'null'. The program ends, at which point nothing is gc'd.
If we assume the mock exam author treated string literals as objects eligible for gc (which is usually the case), I'm not sure how they arrived at the answer of 'one object being eligible for gc'.
There are two strings created in the String pool. "Java" and "JavaScript". The reference variable 's' originally points to "Java" then is reset to point to a new string "JavaScript". The statement <code>String x = "Java";</code> will not create a second "Java" object; a reference to the existing pool object will be returned. Reference 'y' is then given x's reference, so it will now point to the original string object "Java".
At the end of the method you end up with 'y' holding a reference to "Java" and 's' holding a reference to "JavaScript". If string constants were eligible for gc; both these objects would be available when the method completes.
Most of the mock exams ignore the fact that gc is system dependent and use literal strings for the gc examples. For the purpose of the mocks, the majority assume String literals are collected. However; it is highly unlikely you will get a gc question on the exam that involves String literals!. Sun will not include questions that rely on knowledge of a specific JVM implementation!
Also, don't get yourself in knots over gc ... the majority of people report they only had one gc question on the exam
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic