Updated March 10, 2023
Introduction to Memory Allocation in Java
Memory allocation in java can be defined as a process of assigning storage to java programs or services. Memory allocation in java is done in JVM (Java Virtual Machine) memory, broadly divided into a heap and non-heap memory. This article will detail how heap memory and stack memory, equivalent to non-heap memory, are allocated to Java programs.
How Does Memory Allocation work in java?
As we know, that java is an object-oriented language; therefore, all objects created in java are stored in JVM (Java virtual machine). JVM memory is divided into the following parts:
1. Heap Memory
The java runtime uses Heap Memory to allocate memory to objects and classes while executing a java program. Whenever an object is created in java, it gets stored in heap memory. In addition, a garbage collection process runs on heap memory to free up unnecessary space; garbage collection removes those objects from the heap area that does not have any references. Heap memory in java is divided into the following parts:
- Young Generation: This is the part where all newly created objects are placed. When this part of the java heap gets filled up, a minor garbage collection occurs to free up space.
- Old Generation: All objects left in memory after minor garbage collection are moved into the old generation. Therefore this is the part of heap memory where long-living objects are present.
- Permanent Generation: This part of JVM contains native and static methods that provide metadata for running java applications.
Here are some important points regarding java heap memory:
- If Heap space gets full, the OutOfMemory error is thrown by java.
- Access to Heap memory is slow as compared to stack memory.
- Heap memory is much more in size as compared to stack memory.
- Heap memory is not thread-safe as all objects share it.
- Automatic deallocation is not present in heap memory as it needs a garbage collector to free up space.
2. Stack Memory
As the name signifies, stack memory is based on LIFO (last in, first out) principle. Stack memory is used for static memory allocation, and each executing thread in a java program has its stack memory. Whenever a Java method is called, a new block is created in java stack memory to hold local or intermediate variables and references to other objects in the method. As soon as the execution of the method is completed, the block of memory in the stack becomes empty and used by the next method. Therefore, the Stack memory size is less than heap memory. Here are some of the important features of stack memory.
- Stack Memory grows and shrinks as new methods are added and removed to stack memory.
- Stack memory gets automatically allocated and deallocated after the method completes its execution.
- Access to stack memory is fast as compared to heap memory.
- Whenever stack memory gets full, an exception called stack overflow exception is thrown by java.
- Stack memory is thread-safe as each thread has its stack memory.
Here is a small comparison of stack and heap memory in java:
Heap Memory | Stack Memory |
The entire application uses heap memory during its runtime. | The application in parts uses stack memory. That means it is used one at a time during thread execution. |
Heap memory is larger than stack memory. | Stack memory is small as compared to heap memory. |
All objects created during the application are stored in heap memory. | Stack memory only stores local variables and references to objects. |
Access to heap memory is slow. | Access to stack memory is fast as compared to heap memory. |
Heap memory is allocated by creating new objects and gets deallocated by a garbage collector. | Stack memory is automatically allocated and deallocated with the end in method execution. |
Heap memory stays as long as the application is running. | Stack memory stays only until a method is executing. |
Example of Memory Allocation in Java
Now we will see a java example showing how memory is allocated
Code:
package com.edubca.javademo;
class StudentData{
int rollNumber;
String name;
public StudentData(int rollNumber, String name) {
super();
this.rollNumber = rollNumber;
this.name = name;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
int id = 11;
String name = "Yash";
StudentData s = null;
s = new StudentData(id, name);
System.out.println("Student Id is " + s.getRollNumber());
System.out.println("Student Name is " + s.getName());
}
}
Output:
Now we will see how memory is allocated in the above program:
1. In the Main class, after entering the main method, since id, the name is local variables, a space in stack memory is created in the following way:
- Integer id having primitive value will be stored in stack memory.
- Reference of StudentData object s is stored in stack memory pointing to the original Student object, which is stored in heap memory.
2. Call to StudentData class constructor will be added to the top of stack memory. As a result, the following will be stored:
- Reference to calling object.
- Integer variable id having value 11.
- Reference of String type variable name will point to the object stored in the string pool in heap memory.
3. Two instance variables declared in the StudentData class will be stored in heap memory.
Recommended Articles
This is a guide to Memory Allocation in Java. Here we discuss how memory allocation is achieved in java with the help of stack and heap memory. You may also have a look at the following articles to learn more –