Let me try.. The heap is an area of pre-reserved memory that a program process can use to store data. In Java, all the new()'ed objects are on the heap, including new()'ed String and StringBuffer. The process manages its allocated heap by requesting a "chunk" of the heap (called a heap block) when needed, returning the blocks when no longer needed, and doing occasional "garbage collecting," which makes blocks available that are no longer being used and also reorganizes the available space in the heap so that it isn't being wasted in small unused pieces. In Java, objects that have no active references are garbage collected. A pool is an area in the memory too, but it is less dynamic compared to the heap since objects live longer in the pool, and are subjected to garbage collection only when the class is unloaded. A pool is where JVM stores and manages constant Strings. Java optimizes memory utilization by NOT creating identical strings, but reusing the ones in the pool. Any String object created and initialized using the = operator is added to the pool, instead of the heap. Other than String objects, no other kind of objects live in the pool. This includes StringBuffer and any other Java object. Hope this helps, if not please be more specific as to what exactly you don't understand Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).

Originally posted by Ajith Kallambella:Let me try.. The heap is an area of pre-reserved memory that a program process can use to store data. In Java, all the new()'ed objects are on the heap, including new()'ed String and StringBuffer. The process manages its allocated heap by requesting a "chunk" of the heap (called a heap block) when needed, returning the blocks when no longer needed, and doing occasional "garbage collecting," which makes blocks available that are no longer being used and also reorganizes the available space in the heap so that it isn't being wasted in small unused pieces. In Java, objects that have no active references are garbage collected. A pool is an area in the memory too, but it is less dynamic compared to the heap since objects live longer in the pool, and are subjected to garbage collection only when the class is unloaded. A pool is where JVM stores and manages constant Strings. Java optimizes memory utilization by NOT creating identical strings, but reusing the ones in the pool. Any String object created and initialized using the = operator is added to the pool, instead of the heap. Other than String objects, no other kind of objects live in the pool. This includes StringBuffer and any other Java object. Hope this helps, if not please be more specific as to what exactly you don't understand Ajith

Coming from a C/C++ background, I would imagine the Pool is an area of the stack where all constants and literals for the class are stored. If this is correct, not only Strings, but all constants (final in Java) would be stored here. In C/C++, when you use new() or malloc you get memory from the heap, but if you initialize it statically, it goes on the stack. So, heap is dynamic memory and Stack is static memory. When the program exits, the stack variables are gone, but the new()ed or malloc() ed memory still exists in pointers. This is just speculation on my part - could this be correct? Savithri

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782

posted Aug 14, 2000 06:26:00

0

Savithri, The nice thing about Java is, you don't have to worry about creating/freeing memory resources. Though you can create objects using the new() method, once it is created, you simply use it. The Garbage collector will take care of cleaning up when the object has no references. Having said that, it is immaterial whether the pool can be interpreted as stack. Think of the pool as another part of the heap which is visited by the Garbage Collector only once, ie., when the class is unloaded. IMO pool is not on the stack. Ajith

Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169

posted Aug 14, 2000 11:38:00

0

Ajith, That means if create a string using new(), will that string object be only in heap and not in pool and vice vesrsa?? - Thanks

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782

posted Aug 14, 2000 11:48:00

0

new() creates objects on the heap. String s = "Hello" creates the string in the pool. String s = new String("Hello") creates it on the heap. As far as I know, apart from constant strings, nothing else exist in the pool and that's why most of the times it is referred to as the string pool. Ajith

There's more than one type of pool. There are indeed runtime constant pools, one per class, which contain all the literals used by that class. Not just Strings, but also any numeric constants, and a few other things that the JVM uses. Runtime pools are described in the JVM Specification in several places, starting with section 3.5.5. There is also a pool specifically for interned strings, called either the "string pool", "interned string pool", or simply "intern pool". This one contains strings only - specifically those strings which either (a) are also literals, or (b) have been returned by the String.intern() method. This is most clearly described in the API for String.intern(). It's not immediately clear just how these pools are related. My interpretation is that the pools contain lists of references to String objects - the objects themselves may be elsewhere, quite possibly on the heap. So if three classes A, B, and C all mention the literal "Ajith", then the heap will have a single instance of the String object "Ajith", and there will be at least four references to it - one in each of the class runtime constant pools, and one in the interned String pool.

[This message has been edited by Jim Yingst (edited August 14, 2000).]