Memory(Stack vs Heap)

What is RAM?

Computer random access memory (RAM) is one of the most important components in determining your system’s performance. RAM gives applications a place to store and access data on a short-term basis. It stores the information your computer is actively using so that it can be accessed quickly. 

The more programs your system is running, the more you’ll need RAM. The speed and performance of your system directly correlate to the amount of RAM you have installed. If your system has too little RAM, it can be slow and sluggish.

In this article, we are discussing Stack vs Heap which is that Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the RAM.

What is Memory?

Memory in a computer is just a sequential set of “buckets” that can contain numbers, characters, or boolean values. By using several buckets in a row, we get arrays. By giving names to a set of contiguous buckets, we get a “structure”. But at its core, a computer memory is a very simple list of numbers. Everything else must be built up upon this.

Memory Layout

  1. Memory is laid out in sequential order basically from 0 on up (one byte at a time). Each position in memory has a number (called its address!).
  2. The compiler (or interpreter) associates your variable names with memory addresses.
  3. In some languages like C, you can actually ask the computer for the address of a variable in memory. In C this is done using the ampersand &In many languages, the actual address is hidden from you and is of little use to you, as all the access methods “abstract” the details of the computer hardware away, allowing the programmer to concentrate on the algorithm, and not the details.
  4. Arrays variables simply contain the address of the first element of the array. Arrays are zero based so the address simply becomes the base address plus the index.
  5. Structure variables simply contain the address of the first element of the structure, and each “named” field of the structure forms an offset from the first bucket. The computer keeps track of this offset so that the programmer can use symbolic names instead of numbers.
  6. Memory buckets are 8 bits long (or one byte). A character (char) is one byte. An integer is (usually) four bytes. A float is four bytes. A double is 8 bytes.

What is a Stack?

A stack is a special area of computer’s memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime.

It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased. The stack section mostly contains methods, local variable, and reference variables.

The stack is a LIFO (Last-In-First-Out) data structure. You can view it as a box of perfectly fitted books — the last book you place is the first one you take out. By using this structure, the program can easily manage all its operations and scopes by using two simple operations: push and pop.

These two do exactly the opposite of each other. Push inserts the value to the top of the stack. Pop takes the top value from it.

To keep track of the current memory place, there is a special processor register called Stack Pointer. Every time you need to save something — like a variable or the return address from a function — it pushes and moves the stack pointer up. Every time you exit from a function, it pops everything from the stack pointer until the saved return address from the function. It’s simple!

What is a Heap?

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation.

Heap is created when the JVM starts up and used by the application as long as the application runs. It stores objects and JRE classes. Whenever we create objects it occupies space in the heap memory while the reference of that object creates in the stack. It does not follow any order like the stack. It dynamically handles the memory blocks. It means, we need not to handle the memory manually.

For managing the memory automatically, Java provides the garbage collector that deletes the objects which are no longer being used. Memory allocated to heap lives until any one event, either program terminated or memory free does not occur. The elements are globally accessible in the application. It is a common memory space shared with all the threads. If the heap space is full, it throws the java.lang.OutOfMemoryError

Differences between Stack and Heap

Parameter Stack Memory Heap Space
Application It stores items that have a very short life such as methods, variables, and reference variables of the objects. It stores objects and Java Runtime Environment (JRE) classes.
Ordering It follows the LIFO order. It does not follow any order because it is a dynamic memory allocation and does not have any fixed pattern for allocation and deallocation of memory blocks.
Flexibility It is not flexible because we cannot alter the allocated memory. It is flexible because we can alter the allocated memory.
Efficiency It has faster access, allocation, and deallocation. It has slower access, allocation, and deallocation.
Memory Size It is smaller in size. It is larger in size.
Java Options Used We can increase the stack size by using the JVM option -Xss. We can increase or decrease the heap memory size by using the –Xmx and -Xms JVM options.
Visibility or Scope The variables are visible only to the owner thread. It is visible to all threads.
Generation of Space When a thread is created, the operating system automatically allocates the stack. To create the heap space for the application, the language first calls the operating system at run time.
Distribution Separate stack is created for each object. It is shared among all the threads.
Exception Throws JVM throws the java.lang.StackOverFlowError if the stack size is greater than the limit. To avoid this error, increase the stack size. JVM throws the java.lang.OutOfMemoryError if the JVM is unable to create a new native method.
Allocation/ Deallocation It is done automatically by the compiler. It is done manually by the programmer.
Cost Its cost is less. Its cost is more in comparison to stack.
Implementation Its implementation is hard. Its implementation is easy.
Order of allocation Memory allocation is continuous. Memory allocated in random order.
Thread-Safety It is thread-safe because each thread has its own stack. It is not thread-safe, so properly synchronization of code is required.


Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *