Trending September 2023 # Types Of Memory In Java # Suggested October 2023 # Top 18 Popular |

Trending September 2023 # Types Of Memory In Java # Suggested October 2023 # Top 18 Popular

You are reading the article Types Of Memory In Java updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Types Of Memory In Java

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 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 5. Native Area Examples of Memory in Java

Now we will see a java example showing how memory is allocated:


package com.edubca.javademo; class StudentData { int rollNumber; String name; public StudentData(int rollNumber, String name) { super(); this.rollNumber = rollNumber; = name; } public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String 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()); } }


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 –

Java Period

Functional Interface in Java

You're reading Types Of Memory In Java

Update the detailed information about Types Of Memory In Java on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!