Stack and Heap


Object-Oriented Programming
What is OOP?

The course is part of this learning path

Start course
1h 27m

In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.

Learning Objectives

  • Object-oriented programming in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there my friends. In this video, we will talk about the stack and heap in Java. Stack and heap are two ways in which Java allocates memory. In this section, we'll understand how they work and when to use them for developing better Java programs. In Java, memory management is a vital process. It's managed by Java automatically. The JVM divides the memory into two parts: stack memory and heap memory. From the perspective of Java, both are important memory areas, but they are used for different purposes. The major difference between stack memory and heap memory is that the stack is used to store the order of method execution and local variables, while the heap memory stores the objects and it uses dynamic memory allocation and deallocation. The stack memory is a physical space in RAM allocated to each thread at runtime. It's created when a thread is created. Basically, local variables, methods, primitive data types, and references of objects are stored in the stack space of memory.

Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and references to other objects in the method. Memory management in the stack follows the LIFO, Last In First Out order, because it is accessible globally. It stores the variables, references to objects, and partial results. Memory allocated to stack lives into the function returns. If there is no space for creating the new objects, it throws the java.lang.StackOverflowError. The scope of the elements is limited to their threads. The JVM creates a separate stack for each thread. Heap memory is created when the JVM starts up and is used by the application as long as it runs. Objects and reference types are stored in heap. Whenever we create an object, it's always created in the heap space of memory and stack memory contains the reference to it. It does not follow any order like the stack. It dynamically handles the memory blocks. It means we don't need to handle the memory manually.

For managing the memory automatically, Java provides a garbage collector that deletes the objects which are no longer being used. Memory allocated to heap lives until any one event, either a program terminates or memory freed does not occur. The elements are globally accessible in the application. It is a common memory space shared by all the threads. If the heap space is full, it throws the java.lang.OutOfMemoryError. Stack memory is much smaller than heap memory. So, if you know exactly how much data you need to allocate before compiling time and this data is not too big, you should use the stack. If you don't know exactly how much data you need to allocate at runtime, or if you need to allocate lots of data, then you should use heap. Now, let's try to understand stack and heap through an example. As shown on the slide, let's assume that there are two Java classes named Car and Bike. There are two instance variables of type int in the Bike class: b and c. Now, let's look at the Car class. In the Car class, there is an instance variable of type int with the name d. In addition, an object has been created from the Bike class in the Car class. Now, let's look at the main() method.

An object of the Car class has been created in the main() method. The name of the object is myCar. Now, let's look at the location of these objects, variables, and methods in the stack and heap. We said that methods are stored in stack. Therefore, we wrote the main method in the stack. In addition, an area representing each class, or rather an object, was created in the heap. For this reason, we have determined two areas named Car and Bike in the heap. Also, instance variables in classes were stored in these fields. Therefore, I wrote the variables b and c  in the space reserved for Bike, and the variable d in the space reserved for Car. Finally, let's look at the references. We created a myCar object that references to the Car class in the main() method. References were stored in the stack. That's why I wrote the myCar object in the stack section. Also, since this myCar object references the car class, I tried to represent this reference with an arrow. Finally, we created a Bike object in the Car class that references the Bike class.

Since this object is of the Car class, I wrote it here. But the reference it gives is the Bike class. That's why I marked the Bike class with an arrow. So, to summarize briefly, when a method is called, a frame is created on the top of the stack. Once the method has completed execution, the flow of control returns to the calling method and its corresponding stack frame is flushed. Local variables are created in a stack, instance variables are created in a heap, and are part of the object they belong to. Reference variables are created in a stack. So, now you know how Java handles memory management. Let's take a quick break here and I'll see you in the following video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.