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.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsAs 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:
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:
Here are some important points regarding java heap 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.
Here is a small comparison of stack and heap memory in java:
|
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. |
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:
2. Call to StudentData class constructor will be added to the top of stack memory. As a result, the following will be stored:
3. Two instance variables declared in the StudentData class will be stored in heap memory.
The above is the detailed content of Memory Allocation in Java. For more information, please follow other related articles on the PHP Chinese website!