• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pool and Heap's of Confusion

 
Yogesh Mhatre
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been refering to this site from last 2/3 days and came across the following terminology hard to digest can anyone pls explain me what does "String pool" and "Heap" actually mean
 
Fawad Khan
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogesh,
"Heap" is a virtual area of memory where the original value of the object created is stored while the referrence of that is stored in the "Stack" memory area.
"Pool of String" refers to the area in memory where the the String ( which is stored as an array ) is kept and that does not allow two strings with the same value, rather that creates 2 referrence of that same string value. And this resides in the "Heap" memory area.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects are created on the heap. Garbage collection is done to keep the heap clean.
Variables are created in the stack. Each method has it's own stack. If it is a local variable, when the method completes, the stack for that method is discarded. The garbage collector does not do this. The variable will be discarded whenever the variable goes out of scope.
However the String literal "XYZ" will get created at class load time in the Constant Pool, the garbage collector will not get rid of that. String Literals are NOT objects. The Constant Pool is a separate area of memory from the heap and the stack.
 
Yogesh Mhatre
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Cindy
when u say tht----
"for a local variable, when the method completes, the stack for that method is discarded. The garbage collector does not do this. The variable will be discarded whenever the variable goes out of scope.-------
do you mean tht the local variables like the String literals on the String pool are never garbage collected
Yogesh
[This message has been edited by yogesh mhatre (edited February 21, 2001).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are disposed of, but not by the garbage collector . Well, technically there is nothing preventing a vendor from creating a JVM that uses the garbage collector to clean up stacks, it would just be very inefficient. When the system is done with the stack for the method, the entire stack gets zapped by the system instead of the garbage collector utility.
The constant pool is not garbage collected. But constants are tiny compared to objects so it is no big deal.
 
nan sh
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cindy,
If as you said that string literals created in constant pool
, if I have an application which deal with a lot of string literals(said,50k of string literals), I have to use StringBuffer, otherwise memory will dry out, is it right?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, most implementations of the JVM put the constant pool in ROM (Read only memory = very efficient, that's why java uses String Literals instead of String objects as often as it can) so it does not compete in any way for the RAM that the Heap and the stack use.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic