Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable objects garbase collected?

 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have got a confusion regarding the garbage collection of any immutable class including String,Integer etc. Can anyone give me one small example explaining this.

If do the following :

Case 1:
String s1 = "123"; //line 1
s1 = "abc"; // line 2
//line 3
At line 3 how many objects are elligible for garbase collection.

Case 2:
String s2 = new String{"123"); //line 1
s2 = new String("abc"); // line 2
//line 3
At line 3 how many objects are elligible for garbase collection.

Case 3:
Integer i1 = new Integer("123"); //line 1
i1 = new Integer("567"); // line 2
At line 3 how many objects are elligible for garbase collection.

Appreciate your help.

Thanks
Arnab
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to that if someone could throw some light upon as to how many objects are actually created by this line

String s2 = new String("123");

I guess it should be two. One in the literal pool (123) and the other one created using new operator.
 
Sac Anand
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to that if someone could throw some light upon as to how many objects are actually created by this line

String s2 = new String("123");

I guess it should be two. One in the literal pool (123) and the other one created using new operator.


I believe only one object will be created and that too not in pool.

String str = "ONE"; .... This will create in pool .. when you use new operator, it will not be created in pool.
 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right that String s = "dasdsa"; will create only one object and that too in pool bu with String s1 = new String("dasda"); will create one object in non-pool and the one reference in pool which will refer to non-pool object.

But I think we are diverting from the actual question that I have put. If anybody can explain me when these will be garbase collected, I will be thankful.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = "dasdsa";
Will only create a new object in the String pool if no other Object in the String pool currently exists with that sequence of characters. The real exam does not focus on String pool issues with regard to Garbage collection or numbers of objects created, as it is somewhat of an exception to the general rule on object creation.
 
Arnab karmakar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you can tell me whether immutable objects are garbage collected with one simple example it will be great.

Thanks
Arnab
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arnab karmakar:
But if you can tell me whether immutable objects are garbage collected with one simple example it will be great.

Thanks
Arnab


Well I can just have a try...

Moderators if find any problem then they may correct me..

First try this program..

The output will be:
Before: 1918120
After: 1916896
Memory Used for String: 1224


Now try this code:



Now the output is:

Before: 1918104
After: 1916880
Memory Used for String: 1224


As we declare another literal in literal pool second time so if it wouldn't have been collected then it must show

Before: 1918104
After: 1915656
Memory Used for String: 1224


That means that literal "123" is being collected by Garbage Collector....

PS: Moderators pls correct me if I wrote wrong code..to analyze the things properly..
[ September 26, 2006: Message edited by: Ankur Sharma ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic