image
Stack & Heap
Start course
Difficulty
Beginner
Duration
2h 5m
Students
188
Ratings
4.6/5
Description

In this course, we'll learn the object-oriented concept in Java.

Learning Objectives

  • Object-Oriented programming concept
  • Object & Class
  • Access Modifiers
  • Naming Conventions 
  • Constructors
  • Packages
  • Static Keyword
  • Nested and Inner Classes

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. 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 both 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 the 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 reference 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 in the function's 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 the application 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 one event, either program terminated or one memory free does not occur. The elements are globally accessible in the application. It's a common memory space shared with all the threads. If the heap space is full, it throws the java.lang.OutOfMemoryError. Stack memory size is very less compared to 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 seen in the slide, let's assume that there are two Java classes named Car and Bike. There are two instant 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 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. To summarize briefly, when a method is called, a frame is created on top of the stack. Once a 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 the stack. Instance variables are created in the heap and are part of the object they belong to. Reference variables are created in the stack. The memory management in Java is like this. Let's take a short break here. See you in the next video.

 

About the Author
Students
3984
Courses
64
Learning Paths
5

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.

Covered Topics