aspose file tools
The moose likes Java in General and the fly likes Why only Strings are handled in a different kind of memory pool? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why only Strings are handled in a different kind of memory pool?" Watch "Why only Strings are handled in a different kind of memory pool?" New topic
Author

Why only Strings are handled in a different kind of memory pool?

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 362

Hi all,

I have this question, may be this was answered so many times by you guys. but I hope to get a satisfactory answer again.

(1) Why only Strings are handled in a different kind of memory pool?
(2) why they are made immutable, and How Java has implemented immutability in Strings?
(3) How can we make an object immutable?

Thanks,
Pawan Chopra.


Pawan Chopra
SCJP - DuMmIeS mInD
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

(1) Strings are not handled in a different kind of memory pool. The contents of String objects is stored on the heap, just like any other kind of object in Java. What you probably are referring to is the string pool, which is an optimization technique to reduce the amount of memory that Java programs use. The string pool is not a special kind of memory pool that's different from the heap or the stack.

(2) Read this article to understand what "immutable" means. String objects are immutable because of the way the String class is constructed. If String objects were not immutable, then an optimization technique like string pooling would not be possible.

(3) Read the article that I mentioned above. To make your own class immutable, don't provide any methods that change the state of an instance of your class after it has been created (so, don't provide any setter methods or other methods that change the internal state of the object).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 362

For first question I am asking that why a different string pool for Strings only? why not for Double or any other object?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Integers can get pooled too, and Booleans should always attempt to use Boolean.TRUE and Boolean.FALSE, although that is not quite the same.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 362

I think I am not able to explain my question. I know that we can also pool Integers and Doubles. But why Java has handled pooling for Strings only not for Integers or Doubles when they can be pooled?


Sorry If It is irritating you guys
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by pawan chopra:
I think I am not able to explain my question. I know that we can also pool Integers and Doubles. But why Java has handled pooling for Strings only not for Integers or Doubles when they can be pooled?


Because the difference in memory usage is much bigger for Strings.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

But Java does have special treatment for Integer.valueOf(...)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Mutable and Immutable Objects

Discussing the immutable tricks from Strings is not so simple as it employs tricks we can only dream of. Feel free to browse the source code.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
There can be a lot of memory chewed up by string usage, and there is a lot of value to be saved by pooling that memory. Especially given typical programmatic manipulation that occurs on strings.

Int/Double are of fixed size, and a lookup for all ints would be much less productive. You usually want to optimize processing speed when working with numbers, moreso than memory usage.

You easily could decided to use a similar scheme, but the Masters of Java� have weighed the pluses and minuses and not gone that direction.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

Another thing that hasn't been mentioned here is that String object literals have been built into the Java language since day one: i.e., you write

System.out.println("Hello, World");

and this refers to a String object whose value is "Hello, World". If your code uses "Hello, World" a couple of times, it would be silly to create a separate String for each occurrence: thus the pool. You see how this fell naturally out of the language itself. Strings are special because there traditionally were no other kind of "invisible objects" like this, that appeared seemingly out of nowhere.

But since Java 5, we've had autoboxing, and so Integer, Double, etc objects can all be created invisibly. And so, in fact, there are pools for these too (as someone did point out above.) These new pools are accessed via the "valueOf()" methods in each wrapper class. Integer.valueOf(3) will return a pooled Integer object with value 3. Therefore the String pool is no longer unique.

As to immutability requiring "tricks we can only dream of", I'm not quite sure what that means. It's easy to make an immutable class: just don't provide any way to change the innards of the object!


[Jess in Action][AskingGoodQuestions]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
[Jesper]: (1) Strings are not handled in a different kind of memory pool. The contents of String objects is stored on the heap, just like any other kind of object in Java. What you probably are referring to is the string pool, which is an optimization technique to reduce the amount of memory that Java programs use. The string pool is not a special kind of memory pool that's different from the heap or the stack.

That's what the documentation for String.intern() indicates, but it's not really true, at least for modern JVMs. Strings in the intern pool are in the heap, true, but it's a special part of the heap - the permanent generation. This is an area for objects that are assumed to stay around in memory for a long time - though contrary to the name, objects here aren't necessarily permanent, just very long-lived.

Additionally, the string intern pool consists of two components: the strings themselves, and a hash table that contains references to the strings. This hash table is part of the JVM infrastructure, implemented in native code, not a Java HashMap or other Java object. Not in the heap at all, really.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Why only Strings are handled in a different kind of memory pool?
 
Similar Threads
String Objects created
Reasons for using StringBuffer or StringBuilder classes rather than String
Stings: how many objects created?
Objects on Heap
String confusion