Search This Blog

Saturday, February 17, 2007

How does Java allocate stack and heap memory? Explain re-entrant, recursive and idempotent

Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like
int and double are allocated in the stack, if they are local method variables and in the heap if they are member
variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked
and stack pointer is decremented when a method call is completed. In a multi-threaded application each thread
will have its own stack but will share the same heap. This is why care should be taken in your code to avoid any
concurrent access issues in the heap space. The stack is threadsafe (each thread will have its own stack) but the
heap is not threadsafe unless guarded with synchronisation through your code.


A method in stack is reentrant allowing multiple concurrent invocations that do not interfere with each other. A
function is recursive if it calls itself. Given enough stack space, recursive method calls are perfectly valid in Java
though it is tough to debug. Recursive functions are useful in removing iterations from many sorts of algorithms. All
recursive functions are re-entrant but not all re-entrant functions are recursive. Idempotent methods are methods,
which are written in such a way that repeated calls to the same method with the same arguments yield same
results. For example clustered EJBs, which are written with idempotent methods, can automatically recover from a
server failure as long as it can reach another server.




18 comments:

Anonymous said...

Hi The image is not readable.

Anonymous said...

i agree the image is not readable, i think it wud be much helpful if it was.

javapeople said...

Javapeople

I tried increasing the size of image. but It doesn't fit on my blog

Anonymous said...

This is very nice information i have found here. Thanks.

Anonymous said...

copied from a book, shame!

Anonymous said...

Picture is not clear. Can you please send this heap pic? Thanks.
My id is rnandyala@gmail.com

SUREE said...

hi,
can you post related articles and this image to suree2k2@yahoo.com id.

Regards,
Suree.

Sandeep Garg said...

Hi, nice article
Picture is not clear. Can you please send this heap pic? Thanks.
My id is sandeepgarg.dot@gmail.com

deeps said...

Hi,

Nice information..... can you plz forward the related info to rachumallu.sri@gmail.com

Anonymous said...

Excelent article. Thanks.

Anonymous said...

Picture problem

Anonymous said...

thanks..awesome and simple explanation..cleared most of my doubts...

Anonymous said...

Short and sweet. I liked the explanations. Good Job....does'nt matter if it has been copied from a book...what matters is that I can find it with a few keystrokes and google :)

Anonymous said...

Information provided here helped me a lot .....
Thank you guys..

Anonymous said...

Got something new to know....Thank you guys..

jasmeet said...

pls send me name of the book and also send me the image of the above photo
my id -jasmeet0013@gmail.com

Dhaval Patel said...

Thanks Nice ..

Anonymous said...

Siva here:
Expected Clear picture in this site:
http://dnhome.wordpress.com/2012/06/28/how-does-java-allocate-stack-and-heap-memory/