What is difference between heap memory and stack memory?

already exists.

Would you like to merge this question into it?

already exists as an alternate of this question.

Would you like to make it the primary and merge this question into it?

exists and is an alternate of .

Difference between Stack vs Heap memory"Stack memory" and "Heap memory" are physically the same.
The same chip of RAM may be used as stack memory when running one program and later used as heap memory when running some other program.
The difference is in how they are used.


Often a function or method calls another function which in turn calls another function etc.
The execution of all those functions remains suspended until the very last function returns its value.

All the information required to resume the execution of these functions is stored on the stack.

In particular, local variables are stored on the stack.
Local variables are often stored for short amounts of time while a function/method block uses them to compute a task.
Once a function/method has completed its cycle, the space on the stack used by all local variables is freed.

This chain of suspended function calls is the <b>stack</b>, because elements in the stack (function calls) depend on each other.

The stack is important to consider in exception handling and thread executions.


The heap is simply the memory used by programs to store global variables.
Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.

All global variables are stored in heap memory.

All variables dynamically created by the program with "new()" or "malloc()" or similar commands are also stored on the heap.

In some programming languages, all instances of an object, including all the attributes of that instance, are stored on the heap.
In those programming languages, local variables of a function that have object type are implemented as creating the new object on the heap,
and storing a reference to that object in the local variable, which is on the stack.
When that function exits, the heap memory used by each local variable that has object is freed, and then all the stack used by that stack is freed.

A Heap reference is also stored in Stack memory until the life cycle of the object has completed. Inside the Heap reference all the the contents of the object are stored whereas with a local variable only the variable contents are stored in the stack.

var blue
var red
ref 0x456783 (Heap reference)
var tom
ref 0x498702 (Heap reference)
var diane

Heap (0x456783)
name => Susan
age => 26
city => London
height => 5'7
sex => female

Heap (0x498702)
name => Paul
age => 21
city => Glasgow
height => 6'0
sex => male

In addition to heap memory and stack memory, a completely separate section of memory stores the constructors and other methods of a class/object.
34 people found this useful

What is the difference between RAM and memory?

There is none . Computer memory is called RAM, which is an acronym for Random Access Memory. It's called "random access" because data can be recalled from any memory locati

What is the difference between register stacks and memory stacks?

Registers are normally memory spaces internal to the processor or very close to it. They are generally faster than main memory and will be small in size and will hold very fre

What is the difference between register and memory stack?

Registers are momentary spaces for internal processes that arebeing done by the processor. While, memory stacks are a series ofmemory spaces that is used in the processes that

Differences between memorials and rituals?

its hard to explain but memorial is somethin you remember like memorial day or a landmark rembering fallen soldiers. a ritual is somethin you praise and worship like voodoo ma
In Computer Memory

What is the difference between memory and your harddrive?

Well, the Hard Drive (HDD) is a form of memory called "storagememory." On the hard drive, there is a magnetic disk which spinsaround and when you "save" something on your comp
In Computer Programming

What is difference between heap and stack memory in java?

Stack memory is used for variables, and allocated and deallocated in a LIFO (last-in first-out) fashion. That is, if a variable is allocated, it is just assigned a space on th
In Computer Programming

What is difference between stack and heap memory allocation?

stack is memory allocated for temporary variables used bysubroutines . heap is memory allocated for long term data structures (e.g.linked lists, trees) that are likely to cha
In Computer Programming

What is the difference between stack and heap memory in C?

The stack is a local memory reserve where the program storesvariable and function data. The heap is a special memory reservewhere the programmer can dynamically allocate memor