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
nss
Bartholemu Smith
Ranch Hand
Joined: May 08, 2001
Posts: 317
posted
0
0
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
yes my answer was also 0 but ans given is 1 obj eligible for GC jtips Mock 2 Q42
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
Originally posted by Neha Sawant: yes my answer was also 0 but ans given is 1 obj eligible for GC jtips Mock 2 Q42
2 objectws..
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
String literals are not garbage collected. They are created in the String pool (to my knowledge for ever). Maybe the answer is referferring to t object, but it should have said at what point to consider it.
SCJP2. Please Indent your code using UBB Code
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
i think maybe string objects are not garbage collected so the answer should be 0 Feiing where can u find two objects, please explain
Hassan Naqvi
Ranch Hand
Joined: May 03, 2001
Posts: 158
posted
0
ZERO------------>
Always Belive On Logic!!
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
Sorry... yes.. it is zero. I mix up your question with another one.
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
Hi, I'm a newly registered user. Can anybody explain me how there are ZERO objects ready for garbage collection?? -Maulin
All the Strings are String literals and are in the String pool rather than on the heap. Strings in the String pool are not eligible for garbage collection.
If the Strings were created by code like:
String x = new String( "Java" );
then x would reference a String on the heap which would be eligible for gc when there were no more references to it.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rashmi Tambe
Ranch Hand
Joined: Aug 07, 2001
Posts: 418
posted
0
i think the answer 1 is correct.....as t is the only object eligible foe GC.. Im i right???
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
By the way Marilyn... is it true that String s= new String("Hola"); would create two String objects? One literal and the other not interned and pointed to by s
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Originally posted by Rashmi Gunjotikar: i think the answer 1 is correct.....as t is the only object eligible foe GC.. Im i right???
t is referencing the only object. However, t is a reference and will not be gc'd.
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Originally posted by Jose Botella: is it true that String s= new String("Hola"); would create two String objects? One literal and the other not interned and pointed to by s
As far as I understand, Jose, this is true.
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
Is this true, You guys say that string literals are never Gc'ed., then 1. String r=new String("hello"); 2. String s= "Hi"; 3. String a="How"; 4. a=s; 5. a=null; 6. s=null; Are there no objects for gc here after execution of line 6. Getting confused Regards Neha
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
i am waiting for a reply for my previous question. Could anyone help me on this
amit mawkin
Ranch Hand
Joined: Oct 31, 2001
Posts: 73
posted
0
I think the pool funda was implied basically in these cases String s="pool"; String y="pool"; whenever a string object is created a String literal is added into the pool in order to reduce memory consumptions in this case i think 1 object will be there for gc
So in my case 2 objects are available for gc. r is still having ref so is not available for gc Am i right Regards Neha
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
Hi Neha I guess ( I can't tell you for certain) that there is only one pool for the interned Strings: those that were sent the message interned () and the String literals. I continue guessing: once the String was interned (added to pool if not already present) it is not garbage collected because the JVM holds a reference to the pool for the whole duration of the application. I am guessing because JLS does not specify how the string pool works. This is implementation dependant, and this is the reason why I don't think we will be tested about String literals reachability in the exam.
This is diferent from the first example because there is a non literal String. To my knowledge the String object pointed by r in line 1 is made eligible to garbage collection at no line. hope helps
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
Hi Jose, How many objects are created in my question.I guess it is 4 as r creates 2 objects. When r=null then it will be available for gc. And what about the string literals,we all are confused on this. Regards Neha
Rosie Nelson
Ranch Hand
Joined: Nov 06, 2001
Posts: 31
posted
0
Marilyn, It doesn�t make sense when you state that String literals are not eligible for Garbage Collection. This implies that if you create enough string literals, you will eventually kill the JVM as you will run out of memory. It doesn�t sound correct. Where in the JLS is this written?
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
Hi Neha Yes because Marilyn kindly assure us that new String("Hello") would create two String objects, there are four String objects spawned. But, r = null is not in your code! Hi Rosie Try seaching the ranch for a previous program of my own that shows how String literals are not g.c.ed , even if they were local ones! Search with "Jose Botella" in keyword for name. I don't think you are able to write many of these "literalsssssssssssssssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" Well maybe you could try but there is limit for the length of String literals. I haven't tested trying to compile an String literal greater (not really necesary it could be less) than 655336 to see if it produces a compile error. But I remember that the length for the content of a CONSTANT_Utf8_info entry in the constant pool is given by two bytes. This entry holds the content of a String literal. I haven't been able to pinpoint any reference to the String pool in the JLS nor VMS. They avoid mentioning anything like that. The behaviour that they describe regarding String.intern() method and the String literals is according with the supposition of the existance of such a pool. Besides that, in pages 290 and 292 of the book Inside The Java 2 Virtual Machine by Bill Venners it talks about a list of interned String, that looks like the String pool. Anyway I haven't seen any program been able to recycle a String literal.