• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on GC

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • 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
 
Bartholemu Smith
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes my answer was also 0
but ans given is 1 obj eligible for GC
jtips Mock 2 Q42
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ZERO------------>
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... yes.. it is zero.
I mix up your question with another one.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm a newly registered user. Can anybody explain me how there are ZERO objects ready for garbage collection??
-Maulin
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the answer 1 is correct.....as t is the only object eligible foe GC..
Im i right???
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am waiting for a reply for my previous question.
Could anyone help me on this
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

------------------
coffe drinker
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

1. String r=new String("hello");
2. String s= "Hi";
3. String a="How";
4. a=s;
5. a=null;
6. s=null;

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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic