Forums Register Login

Stack n Heap

+Pie Number of slices to send: Send
Hi,

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

Thankz
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ...
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
The object REFERENCE is stored on the stack, but the object itself is in the heap ( think).
+Pie Number of slices to send: Send
 

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.
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 748 times.
Similar Threads
Heap / Stack
When will a variable use memory from stack and when will use memory from heap?
Where do static variables (class variables) live (scope)?
Where do primitive types live?
JVM memory management
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:57:31.