• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

strings, string pool and garbage collection

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"?
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no object is eligible here

avi sinha
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic