aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Heap and Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Heap and Pool" Watch "Heap and Pool" New topic
Author

Heap and Pool

Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
Can some one briefly explain these terms. I do not have anything
about the book i am refering to.
Heap and Pool in case of Strings. Explain with respect to StringBuffers also.
- thanks in advance.
Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
Please guide me..
Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
Come on...
tinks
Greenhorn

Joined: Feb 23, 2002
Posts: 27
please some one relpy his question. I also want to know the answer. thanks.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
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
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
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
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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).]


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Heap and Pool