TIL-8: Stack and Heap: In a Nutshell

Recep İnanç
4 min readDec 4, 2019

--

Photo by Maël BALLAND from Pexels

“Today I learned what stack and heap are and what are the differences between them.”

Stack

The stack is a region of RAM (random-access memory) that is allocated for temporary variables that are created by functions and it is used for static memory allocation.

The stack is attached to a thread so the scope of the variables in the stack is limited to the thread they are running on.

It is managed by the CPU. It stores the variables created by the functions in the Last-in-First-out (LIFO) format and frees all the allocated memory when the function exits.

The stack is smaller in size compared to the heap but it is faster to read/write and more advantageous since the memory is managed for you. And the stack is faster because memory allocation/deallocation is more trivial compared to the heap.

— When do you use the stack?
+ When you know how much data you need to allocate before compile time and you want to store small-sized local variables and need to access/allocate/deallocate fast.

Heap

The heap is a region that occupies a larger portion of the RAM. It is used for dynamic memory allocation.

The heap’s scope is the whole application, it can be accessed from any part of the application. Elements of the heap are independent of each other and can be accessed randomly at any time. In a multi-threaded environment, each thread will have its own completely independent stacks but they will share the heap.

The heap size is only limited by the size of the virtual memory.

The management of the heap is not handled by the CPU as tightly as it was for the stack, so it is a bit slower and requires manual allocation/deallocation and freeing of the unused parts. If this management is not done correctly issues like Memory-leak may occur that set aside some part of the memory and it won’t be available to other processes.

— When do you use the heap?
+ When you don’t know how much data you will need at runtime and you want to store bigger sized variables that you want them to be globally accessible.

Differences

Java Specific Notes on Stack and Heap

  • Java Heap space is used for Objects and JRE classes. Whenever a new object is created it is stored in the heap.
  • Heap space is managed by two concepts: Garbage collection and young-generation, old-generation.
  • Garbage collection runs on the heap memory to free objects that don’t have any references.
  • Young-generation, old-generation helps prioritize objects for garbage collection by dividing Java Heap Space into two generations.
  • Java Stack memory is used for the execution of a thread.
  • If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.
  • -Xss is used to define the startup size of each thread’s stack memory.
  • -Xms and -Xmx used to define the startup minimum and maximum sizes for the heap memory.

References

📝 Read this story later in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Recep İnanç
Recep İnanç

Written by Recep İnanç

Software Developer @Amazon. You can find me at recepinanc.com. My opinions are my own.