File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strings, string pool and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "strings, string pool and garbage collection" Watch "strings, string pool and garbage collection" New topic
Author

strings, string pool and garbage collection

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
When String pool is used, String objects are not created, right? So, if we are asked to determine the number of objects eligible for garbage collection, can we be sure?

for example-



Can we say that three objects will be available for garbage collection - "aakash", "goel", and "aakashgoel"?


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 436

no object is eligible here

avi sinha


SCJP 5.0 SCWCD 5.0
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
avi sinha wrote:no object is eligible here

avi sinha


can you explain why? is it because all the objects are guaranteed to be in the string pool?
thanks

This message was edited 1 time. Last update was at by Aakash Goel

sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 693

Aakash Goel wrote:When String pool is used, String objects are not created, right?


How many String objects are created in the code provided by you??


Regards,Sudipto
SCJP 5 ScjpFAQ JLS
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
sudipto shekhar wrote:
How many String objects are created in the code provided by you??


Thats what I am not sure of. Can you help?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 436

Aakash Goel wrote:
sudipto shekhar wrote:
How many String objects are created in the code provided by you??


Thats what I am not sure of. Can you help?


three objects are in the pool but none of them are eligible coz String or wrapper pool objects are not collected by gc.

avi sinha
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 754

Hi Aakash,

See the program given below.

Source : I created the following java program for explaination purpose.



Output :

not equal
equal
not equal
not equal


After seeing the output we can say that at line 13 there is concatanation of two string literals but JVM checks that if it is available in pool then assign reference stored in pool other wise create new string object.

so in your situation we can say that string "aakashgoel" is eligible for garbage collection.

Correct me if I am wrong.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 693

Ninad Kulkarni wrote:Hi Aakash,

See the program given below.

Source : I created the following java program for explaination purpose.



Output :

not equal
equal
not equal
not equal


After seeing the output we can say that at line 13 there is concatanation of two string literals but JVM checks that if it is available in pool then assign reference stored in pool other wise create new string object.

so in your situation we can say that string "aakashgoel" is eligible for garbage collection.

Correct me if I am wrong.


Nice explanation as well as example.
Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 35

Hi Ninad,

so in your situation we can say that string "aakashgoel" is eligible for garbage collection.

Where do you think "aakashgoel" object is? In the heap or String pool? And where GC is looking for objects?
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 754

Hi Valentin,
My thinking is when JVM checks that "aakashgoel" string is not in the pool then it is newly created on heap and eligible for garbage collection after s = null.
But if it is stored in string pool then none of the object will be eligible for garbage collection.Let see what other members will say about it.
Can anybody here explain where the string "aakashgoel" will store? in pool or elsewhere? Now I have confusion about it.
Valentin Ivanov
Ranch Hand

Joined: Nov 20, 2008
Posts: 35

Ninad Kulkarni wrote:My thinking is when JVM checks that "aakashgoel" string is not in the pool then it is newly created on heap and eligible for garbage collection after s = null.

Everything in that code is string literal, so as far as I know they are stored not in the heap
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Hi,

First of all, ALL String objects created either through sting literal ".." OR new keyword are stored in the heap. There is NO object string or any other kind of object stored anywhere else BUT in the heap.

The string pool has ONLY references to string objects when the string is created using string literal ".." (for ex. String s="aakash"), if a string object is created using the new keyword THEN there is no reference to that string object in the pool.

Going back to the original code for the question:


1. String s="aakash";
2. s+="goel";
3. s=null;


In the above code there only two strings created, however none of them is garbage collector candidate because they have object references to them in the pool.

I hope this clarifies the issue.

Cheers!!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8325
Sorry to be late to this thread guys!

On the real exam, String objects will NEVER be used in GC-related questions.

So, this is an interesting topic, but IT'S NOT ON THE EXAM

hth,

Bert


Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
Bert Bates wrote:Sorry to be late to this thread guys!

On the real exam, String objects will NEVER be used in GC-related questions.

So, this is an interesting topic, but IT'S NOT ON THE EXAM

hth,

Bert


Thats good news

Thanks

This message was edited 1 time. Last update was at by Aakash Goel

Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83

Aakash, the point is to know and learn Java the proper way not just to depend on what will be included in the exam and what's not.

knowing where the objects and strings reside, and where there references are is very important for you in order to manipulate them successfully, weither it is included in the exam or not.

Cheers!!
Jain Jose
Greenhorn

Joined: Mar 03, 2008
Posts: 3
Hi All,

In the original code snippet,

1. String s="aakash";
2. s+="goel";
3. s=null;

how many String objects are getting created actually. My idea is as follows:

First object is 's' which stores 'aakash'.

Second Object is some anonymous object with value 'goel'

Third object is again 's' since a String object cannot modify its value, we will be automatically creating a new object during this += operation.

Any thoughts on this? Am I completely wrong?
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
Mo Jay wrote:
Aakash, the point is to know and learn Java the proper way not just to depend on what will be included in the exam and what's not.

knowing where the objects and strings reside, and where there references are is very important for you in order to manipulate them successfully, weither it is included in the exam or not.

Cheers!!


you're right Mo Jay.

but considering that I had to give the exam today, and that I was still fuzzy about those concepts, it was very good news for me
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Aakash Goel ,
Good luck for the exam and I hope that you will do very well, keeps us posted on your result and how you did.

In the original code snippet,

1. String s="aakash";
2. s+="goel";
3. s=null;

how many String objects are getting created actually. My idea is as follows:

First object is 's' which stores 'aakash'.

Second Object is some anonymous object with value 'goel'

Third object is again 's' since a String object cannot modify its value, we will be automatically creating a new object during this += operation.

Any thoughts on this? Am I completely wrong?


Sorry Jain Jose but your interpretation is inaccurate as the above code will:
First: create a String object with the value aakash then make the reference s point to it from the stack.
Second: it will create ANOTHER string object with the value: aakashgoel and make the previous s reference point to it, so NOW there is no reference pointing to the first string aakash anymore.
Third: it will make s reference point to null(means point to nothing) and at this point there is no reference pointing to the string object created in part two(aakashgoal). In total, only 2 strings objects were created.

Cheers!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13412

Mo Jay wrote:Sorry Jain Jose but your interpretation is inaccurate as the above code will:
First: create a String object with the value aakash then make the reference s point to it from the stack.
Second: it will create ANOTHER string object with the value: aakashgoel and make the previous s reference point to it, so NOW there is no reference pointing to the first string aakash anymore.
Third: it will make s reference point to null(means point to nothing) and at this point there is no reference pointing to the string object created in part two(aakashgoal). In total, only 2 strings objects were created.


I don't believe Jain's interpretation is inaccurate. The expression "s" is not a compile time constant -- as it is not final (and if it was then we have a compile error). So, there is no way for the compiler to optimize out the need for the "goel" string.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1778
HI Mo Jay,

Its very good explanation...

Mo Jay wrote:Hi,

First of all, ALL String objects created either through sting literal ".." OR new keyword are stored in the heap. There is NO object string or any other kind of object stored anywhere else BUT in the heap.

The string pool has ONLY references to string objects when the string is created using string literal ".." (for ex. String s="aakash"), if a string object is created using the new keyword THEN there is no reference to that string object in the pool.

Going back to the original code for the question:


1. String s="aakash";
2. s+="goel";
3. s=null;


In the above code there only two strings created, however none of them is garbage collector candidate because they have object references to them in the pool.

I hope this clarifies the issue.

Cheers!!


KrishnaSrinivasan (SCJP 1.5, SCWCD 1.3, SCBCD 5.0)
400 Mock Questions for SCJP 1.5, SCJP 1.6, SCWCD 5.0, SCBCD 5.0
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Very nice explanation can be find on a website called JavaRanch: strings

Btw. I don't understand why only two strings are created as said above. What about "goel"? There is no reference to this string at any moment,
but that shouldn't be a problem. Am I right or wrong?


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 754

I have following two conclusions :

1) When s ="aakash"+"goel" then reference of created string "aakashgoel" will go in to string pool.
so none of the objects are eligible for garbage collection.
In this case both strings are compile time constants so they evaluated as string "aakashgoel" , compiler do it at compile time. See Java Languge Specification for details.

2) When s = s + "goel" or s+= "goel" then reference of created string "aakashgoel" will not go in to string pool.
so only "aakashgoel" will be eligible for garbage collection.
In this case one string is compile time constant and another is string reference which is evaluated in runtime.

You can test it using command line option javap [-verbose] [-l] or [-c] ClassFileName then you will see contant table in decompiled code using javap tool.

Correct me if I am wrong.
 
 
subject: strings, string pool and garbage collection
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture