• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference between stack and heap

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!!

first of all...i sincerely hope this is the right forum for this post.

what is the difference between the stack and heap? what goes on the stack, what goes on the heap??
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I hope there will be more posts to add to my input.

The stack is where method being invoked and local variables live.

The HEAP is the garbage collector where ALL Objects LIVE.

Hope this helps...
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate a little, the stack and the heap are just different places in computer memory. Each has a different style of managing memory.

The stack is "cleaned up", that is the local variables and method parameters (among other things) are popped off the stack, when the called method ends. The parameters and local variables no longer exist. (Although, and this is sometimes the confusing part, if a variable or parameter is a reference to an object on the heap, the underlying object does not go away with the stack.)

Objects on the heap, on the other hand, are released from memory by the affectionately named "garbage collector". The garbage collection "destroys" an object when there is no longer any reference to the object.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, suppose i instantiate an object in a method, the object itself is created on the heap, but the reference to it on the stack. and when i return from the method, the reference is de-referenced, and so the object becomes available for garbage collection.

is the amount of memory to both fixed? meaning, in essence, if i run a recursive method with many recursions, i am putting it all on the stack, right? so, if the stack runs out of memory, but the heap has some memory left, can the stack utilize this memory?
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys? some more details??
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct, the object created is in (on?) the heap, and the variable referencing it is on the stack.

When the stack runs out of memory you get a StackOverflowException. You can set the min and max stack size by setting an option when the JVM starts. When the stack runs out of memory it is not allowed to "borrow" memory from the heap, nor elsewhere.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic