Updated March 27, 2023
Introduction to Types of Memory in Java
The Java virtual machine memory area is a runtime area that is used for the execution of various programs involved during runtime of a java application, the memory area of JVM is broadly divided into five different parts which are method area, heap area, Stack, Program counter (PC) registers area and Native method area. In this article, we will discuss the different types of memory in java.
Top 5 Types of Memory 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 basically divided into the following parts:
1. Method Area
All executing threads share this part of the JVM memory area. Class elements like constant pool, class fields, constructor codes, method codes, etc. Method area can be considered as a part of the heap area but stores per-class data only. We can say that the method area is responsible for holding class level information.
2. Heap Memory
Heap Memory in java is used by java runtime to allocate memory to objects and class during a java program’s execution. Whenever an object is created in java, it gets stored into heap memory. A garbage collection process runs on heap memory to free up unnecessary space that is 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, 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.
3. 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 own 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 gets completed, the block of memory in the stack becomes empty and is used by the next method. The size of Stack memory is less as compared to heap memory. Here are some of the important features of stack memory.
- Stack Memory grows and shrinks itself as new methods are added and removed to stack memory, respectively.
- 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 own stack memory.
4. PC Registers
The main function of pc registers is to store the address of currently executing the instruction. It also stores the address of threads responsible for executing current instruction. The size of memory allocated to pc registers is very small. Java applications executing in JVM does not have any effect on pc register memory or its contents.
5. Native Area
This area is implemented using languages other than java. With the creation of new threads, memory is allocated in this area for each created thread. The size of the native area can be fixed or dynamic.
Examples of Memory 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:
Memory Allocation:
Now we will see how memory is allocated in the above program:
- 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 StudentData object, which is stored in heap memory.
- Call to StudentData class constructor will further get added to the top of stack memory. The following will be stored:
- Reference to calling object.
- Integer variable id having value 11.
- Reference of String type variable name which will point to an actual object stored in a string pool in heap memory.
- Two instance variables with the name studentId and studentName declared in StudentData class will be stored in heap memory.
Recommended Articles
This is a guide to Types of Memory in Java. Here we discuss the Introduction, and we have clearly explained different parts of the JVM memory area in detail. You may also have a look at the following articles to learn more –