Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stack n Heap

 
usha prithvi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

can someone explain difference between stack n heap with examples? and when does stack n heap get used internally?

Thankz
 
newgen pras
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ...
Stack is the place where ur primitive local varibles and object references get stored...

Heap is the place where ur object get stored ...

for example
Object ob = new Object();
Object ob1 = ob;

here ob, ob1 will get stored in stack..... the object created by new operator will stay in heap....

Pls correct me if i am wrong any way
 
Pramod Chris
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add, object references are stored in the stack if created within a method, else it is stored in the heap. Objects created are stored on the heap. Stack memory is the programs memory and Heap memory is outside the program.
 
newgen pras
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think object reference will get stored in Heap... If that is going to be stored in Heap ..how it will get garbage collected ..pls explain me ...
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think object reference will get stored in Heap...
The Object reference 'o' is stored on the heap, as a member of the Bar instance.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi newgen

about your comment - "how it will get garbage collected",
I don't think GC would work that way. In fact it does run on "Heap" by starting from top level objects and then following object graphs to see which objects can be GCed.

Regards
Maulin
 
fred rosenberger
lowercase baba
Bartender
Posts: 12266
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object REFERENCE is stored on the stack, but the object itself is in the heap ( think).
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object REFERENCE is stored on the stack, but the object itself is in the heap ( think).
See my previous reply. Local references are stored on the stack. Instance fields (references, primitives, etc.) are on the heap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic