• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Heap and Pool

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please guide me..
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Come on...
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please some one relpy his question. I also want to know the answer. thanks.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic