Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Immutable objects garbase collected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Immutable objects garbase collected?" Watch "Immutable objects garbase collected?" New topic
Author

Immutable objects garbase collected?

Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
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

Joined: Aug 02, 2006
Posts: 109
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

Joined: Apr 21, 2006
Posts: 19
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

Joined: Oct 04, 2004
Posts: 46
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

Joined: Sep 14, 1999
Posts: 2813
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.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
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

Joined: Dec 27, 2005
Posts: 1249

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 ]

The Best way to predict your future is to create it - Every great individual common man
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Immutable objects garbase collected?