Best Answer
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 stack, 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.

User Avatar

Wiki User

โˆ™ 2012-04-30 03:26:54
This answer is:
User Avatar
Study guides
See all Study Guides
Create a Study Guide

Add your answer:

Earn +20 pts
Q: What is difference between heap memory and stack memory?
Write your answer...
Related questions

Difference between Heap memory and Stack memory?

Stack memory is static memory within the program's data segment and is allocated at compile time. Heap memory is dynamic memory allocated upon the free store at runtime.

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

The stack is a local memory reserve where the program stores variable and function data. The heap is a special memory reserve where the programmer can dynamically allocate memory from. The heap is useful when the programmer doesn't know how large to make certain variables, he just constructs one of the right size during run-time.

What is the difference between the stack and the heap?

The stack is a local memory reserve where the program stores variable and function data. The heap is a special memory reserve where the programmer can dynamically allocate memory from. The heap is useful when the programmer doesn't know how large to make certain variables, he just constructs one of the right size during run-time.

What is the difference between allocating memory through heap and malloc?

Nothing, malloc does allocate memory from the heap.

Difference between dynamic and static memory allocation?

Static memory allocation is memory allocated on the "stack" and cannot be resized after the initial allocation, while dynamic memory allocation is memory allocated in the "heap", and can be dynamically expanded and shrunk as necessary.

Why is stack faster than heap?

Stack is not faster than heap. It's all the same physical memory. The difference is only in when the memory is allocated. Each thread of execution has its own fixed-length stack which must be allocated before the thread executes. That memory is therefore readily available to the thread. Heap memory, on the other hand, is free memory that can be shared by all threads of execution, but it must be allocated on an as required basis and released when no longer required. While repeated allocation and deallocation of heap memory can have a significant runtime cost, there's nothing to stop the programmer from providing their own memory manager to reduce the number of allocations/deallocations at the cost of greater memory consumption.

What is in between stack and heap?

Bundle, Perhaps.

Where the Memory is allocated for a variable in a program?

if a variable is of value type memory is allocated on stack memory.. if it is of reference type,memory is allocated on heap memory..

Where are pointer variables stored in C memory?

Pointers can be stored in static memory, on the stack or on the heap, the choice is yours.

Where do objects and references stored in memory in java?

Objects are stored in an area of memory called the "heap", whilst reference variables are stored in the "stack". These are both parts of the computer's RAM memory; the Java Virtual Machine (JVM) simply reserves part of the memory available to it for the stack and for the heap, and manages them accordingly.

Why it is required to create mainframe object on the heap?

You need to create the mainframe object on the heap so that the object does not go out of scope and get automatically deleted when your function exits. You could create it on the stack if the function doing that does not exit until everything is deleted, such as in the main loop of CWinApp. There are also issues with available memory in stack versus heap, as the stack size is preallocated while the heap can grow to the size of available memory.

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 the stack, and the stack pointer is increased; if the method that uses the variable is finished (there is an implicit or explicit "return"), the stack pointer is adjusted downward again. Heap is an area of memory used to assign objects. This is a bit more chaotic; first, objects can have just about any size - small, or large. Second, objects may continue existing after the method that created them ends its execution. On the heap, the Java Virtual Machine just assigns objects in whatever available space it finds. Every now and then, the Garbage Collector does a cleanup, releasing objects from the heap that are no longer used.

Can heap implement recursion?

Heap is a data-structure, it cannot implement anything. On the other hand, it is true that: 1. Recursive routines might use heap. 2. You can use dynamic memory allocation (heap), to implement a stack; and use the stack to implement recursion.

Static and dynamic memory allocation in c plus plus?

int stc; // static int* dyn = new int; // dynamic The difference between the two is that the former is allocated on the stack, in the program's data segement, while the latter is allocated on the heap, in free memory. The stack is very much smaller than the heap, thus the stack should only be used for small and simple variables that are used often but that do not change size (such as small static arrays), while the heap should be used for much larger structures, structures that vary in size (such as dynamic arrays), or temporary structures that are seldom required or highly volatile. Note that both stc and dyn in the example above are allocated on the stack, but the memory pointed to by dyn is allocated on the heap. dyn simply stores the memory address of that allocation.

What is Java heap?

In java when an object of array is created, memory is allocated to them from heap. The JVM through the use of new operator allocates memory from the heap for the object. The JVM has a deamon thread known as Garbage Collector whose task is to free those objects from heap whose reference is not alive in stack.

What is the difference between Fibonacci heap and heap sort?

the name

What is the difference between binary heap and binomial heap?

The difference between Binomial heap and binary heap is Binary heap is a single heap with max heap or min heap property and Binomial heap is a collection of binary heap structures(also called forest of trees).

What is dynamic and static memory?

Dynamic Memory is like Heap from where you can request memory at runtime. Static is predefined memory plan for your source code like Stack space, code space etc.

Is Heap memory is higher than stack?

Not necessarily. It depends on the hardware and the implementation of the operating system's virtual memory manager. Both use the same allocation scheme, but this could be top-down or bottom-up. Also, given that each thread of execution (including the main thread) has its own thread-local call stack, dynamically allocated stacks are allocated on the heap like any other dynamic allocation. Ultimately it makes no difference where stacks are allocated, the point is that it costs nothing to use the stack once it is allocated, whereas explicit allocation and release from the heap incurs a performance penalty unless you provide your own memory manager to reduce the number of allocations (at the cost of increased memory consumption).

What is the difference between insertion sort and heap sort?

Both insertion sort and heap sort are in-place sorting techniques( ie they dont require addition memory space). The main difference between these two techniques is in their efficiency. Insertion sort has an efficiency of O(n2) while heap sort has an efficiency of O(n*log n). Hence heap sort is faster.

Where is memory for class-object allocated?

Normally, on the heap, if you call operator new. It can also be on the stack, if made a local variable.

What is meant by heap in c or cpp?

If you see the word "heap" in the context of C/C++ programming, it is probably referring to one of two ideas. First, if it is written as "the heap", it is probably referring to dynamically allocated memory. We conceptualize memory as either being on "the stack" or "the heap" in main memory. Memory allocation from the heap happens when a call to malloc (or similar functions) are called in C, or when the "new" operator is used in C++. This is in contrast to statically allocated memory, which comes from the load module and is known at compile-time, or from the "stack" which is used at run-time to allocate local scope, or automatic, memory. Another usage of the word heap is a certain data structure called a heap. It is a very common data structure for priority queues and is crucial to the famous HeapSort algorithm. You can easily find more information on this data structure e.g. by searching for HeapSort.

What do you mean by free store in c plus plus?

The free store in any language refers to the heap. The three main areas of memory that all C++ programs use are the heap, the call stack and static memory. Static memory is allocated at compile time, is fixed-length and caters for all static variables, global variables and constant variables. Call stacks are also fixed-length and are allocated to threads of execution as they are instantiated (each thread has its own stack). The free store or heap is essentially all remaining memory accessible to our program. To use the heap we must request memory from the system as it is required and release it when we are finished with it.

Where object is stored on Heap or Stack in java?

an object is stored in a heap in java

What is difference between stack and heap memory allocation?

stack is memory allocated for temporary variables used by subroutinesheap is memory allocated for long term data structures (e.g. linked lists, trees) that are likely to change sizeBoth are forms of dynamically allocated memory (i.e. allocated/deallocated at runtime as needed), but the allocation/deallocation method and their place in physical/virtual memory are differentStatically allocated memory (i.e. allocated at compile/link time) is used for variables and data structures that must exist as long as the program is running and cannot change in size while the program is running.