Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Question in GC

 
Alan Hermin
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers....
i would to know if the object created in the pool is elegible for GC();
for example:
String str="hellow";
Or
Integer i1=5;


Like these: create that in the pool, but is elegible for gc?..
THANKs

[ April 30, 2006: Message edited by: alaa hassan ]
[ April 30, 2006: Message edited by: alaa hassan ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str="hellow"; creates an object on the heap. If you say str=null it will be available for garbage collection.
 
Alan Hermin
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK....But what about the things created in the pool.........
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object is in the pool when you say String str = "hellow"; using the new keyword will create two objects.
 
Nilesh Deshpande
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to confirm my understandings
1. String str="hello";
This will create only one string object
str-->[hello]

2. String str= new String("hello");

This will create 2 string object
unidentified reference-->[hello]
str-->[hello].

Please correct me if I am wrong.
 
Krishna Bulusu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I think Integer i=5 is wrong here.
 
Jegon Jeganathan Renganathan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hasaan,

A good question. As mentioned by one of our friend,
String s= "hello"; //will create only one object in pool
String s = new String("hello");//will create two objects
As far as we are using the first line(which most of us are using) for creating string object, it will be stored in Magical (String pool) which is not eligible for GC.
But, as the second statement which is created in the Heap will be eligible for GC. Hope this will be clear for you.
 
Nilesh Deshpande
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
void test()
{
String str="hello";
String str= new String("hello");
//line 4
}//line 5


After line 5 (i.e. when the function ends), how many objects will be eligible for gargabe collection? I think it will be 2 objects. Since as you mentioned the String str="hello" will not be stored in the heap but will be stored in string pool.
 
gurpreet singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So u mean
String s1 = new String("hello");

creates two objects

where are these 2 objects created?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a good post about string & GC.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic