• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collector  RSS feed

 
Hima Nadi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class sreejith //..1
{ //2
public static void main(String args[]) //3
{ //4
String s="hello"; //5
String s1="hello"; //6
System.out.println(s1); //7
String s3=s1; //8
s1=null; //9
s=null; //10
} //11
I always go wrong on these type of quations.
can someone explain me when objects will be garbge collected.
I know when object is no more in use.
here i presume "hello" in s will be garbage collected in line 10. why i am wrong?

Thanks Hima
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hima Nadi:
class sreejith //..1
{ //2
public static void main(String args[]) //3
{ //4
String s="hello"; //5
String s1="hello"; //6
System.out.println(s1); //7
String s3=s1; //8
s1=null; //9
s=null; //10
} //11
I always go wrong on these type of quations.
can someone explain me when objects will be garbge collected.
I know when object is no more in use.
here i presume "hello" in s will be garbage collected in line 10. why i am wrong?

Thanks Hima

I�m a beginner in Java. I will try to help you anyway.
The s object (the s instance of the String class) will be eligible for garbage collection when it�s no more referenced, as you coded in line 10 for the s instance. This means that the Java Virtual Machine will drop this object by his own criteria.
From line 10 it�s possible s instance to be garbage collected, not before.
Hope that helps.
[ April 17, 2002: Message edited by: Sigfred Zamo ]
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object reference by s is not garbage collected. Because in line 8 it is referred by s3. So, though in line 10 s is initializing by null but the actual object is still referencing by s3. So no garbage collection take pleace...
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Md. Nazmul Huda Sarkar:
.. Because in line 8 it is referred by s3.

s is not referred by s3, s1 is referred by s3, am I wrong?
 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hima,
the answer is no. the object "hello" will not be eligible for garbage collection after u set s to null.
the reason is because it's a String literal so it is created in the StringPool & hence is not eligible for garbage collection.
had ur code been like this
String s = new String("hello");
s = null;
in that case after s = null the "hello" would have been eligible for garbage collection.hope this helps.
-Arun
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigfred, you're correct... but you have to say:
String objects are eligible for GC nor String literals.
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String objects are eligible for GC nor String literals.[/QB]
Have I said somewhere that literals are eligible for gc? I think that not..
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then if you know that the literals are not eligible for GC , Why do you ask you the question above ??
Brief the most important is that we share our knowledge here.
Thanks all for clarification.
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you did�nt notice I didn�t asked the first time.. was Hima, not me...
 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Engin/sigfred,
pls dont fight the sky is not going to fall down. If Valentine notices he may send you both to the MD to fight over.
-Arun
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Valentine notices he may send you both to the MD to fight over.
Hmmmm... Am I that bad
Anyway, there are no fights going on any more in MD and will never be one again. The JR staff has decided that meaningless drivel means meaningless drivel and not digital battlefield
[ April 17, 2002: Message edited by: Valentin Crettaz ]
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn�t know there was a place for fighting.. What do you mean with MD? Actually it exists?
Any way, I apologize if somebody was thinking I wanted to fight, I just wanted to explain myself..
Sorry if I was rude in someway..
 
Hima Nadi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for every one who replied. it was helpfull
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!