• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

what is Stringpool and heap

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

what is stringpool and what is heap

Thanks in advance.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String pool itself a heap. String pool is like a bucket of string constants.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mani can u please ellaborate...
 
Mani vannan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String pool is like a bucket which has string constants.

For instance,
Object a = new Object();
Object b = new Object();
After execution of the above statements, two objects are created and placed on heap.

But,
String s1 = "abc";
String s2 = "abc";
After these two statements, only 1 object is created (on string pool).
Since, they are string constant(objects).

Now,
String s1 = new String("abc");

After above statement is executed, there created two objects.
One object is created on heap (and referenced by 's1').
Another object is created on String pool.

The difference is:
The object created on heap are garabage collectible (when they are eligible).
But, The object created on string pool are never garbage collected.
[ November 16, 2005: Message edited by: Mani vannan ]
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
stringpool - pool of unique string contants or string literals. it will be used by JVM to reuse when same string literal comes.

heap - where objects and member variables are get stored.
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...

String s1 = "abc";
String s2 = new String("abc");
String s3=new String("abc");

How many objects would be there in the string pool....

In heap... i guess it is 2...created by new

Regards
[ November 16, 2005: Message edited by: A Kumar ]
 
Mani vannan
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Totally 3 objects. 1 in string pool, 2 in heap. is that right kumar?
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai mani,
u said objects created in the pool are never garbage collected.But what i think is "they will be eligible for garbage collection when jvm unloads the class".What do u say about this?
 
A Kumar
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is that right



Even i feel that it is the answer but not quite sure....
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String s1 = "abc";
String s2 = new String("abc");
String s3=new String("abc");

How many objects would be there in the string pool....


One.

The String pool is a collection of String objects that have been interned. String literals are automatically interned. So, when the class containing this code is loaded, a String object will be created containing the character sequence ['a','b','c'], and it will be interned. All occurrences of the literal "abc" in this code will refer to the same String object.

With the following lines, two new Strings are created and initialized with the same character sequence. These strings are not pooled.

There will be three distinct String objects created by this code, but only one of which will be pooled.

Hope this helps!
[ November 16, 2005: Message edited by: Steve Morrow ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Ranchers

String s1 = "abc";

will s1 be eligible for Garbage Collection or not?

Thanks in advance,
Rama Krishna Chowdary
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

By the code,



A String object will get created in the heap and reference will be put into the String Pool. By that statement I mean to say is "Programmer wont have any control over that reference." So, whatever the program logic is, "abc" will be pointed by String Pool.

And thats y it wont be garbage collected....

Hope this will help...tell me if I'm goin wrong.

Regards,
Mausam
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for giving feedback.

Regards
krishna
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

will s1 be eligible for Garbage Collection or not?


s1 is a reference variable, not an object; only objects are eligible for garbage collection. In this case, there is a single String object, referred to both by the literal and by the variable s1. Because a strong reference is held to the String object, it is not eligible for garbage collection.
 
It's a tiny ad only because the water is so cold.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic