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"?
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.
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.
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.
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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.
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.
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.
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?
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