JVM Runtime Memory Allocation
In this post, we are going to share
details on JVM memory allocation.It is very important for the developer to understand the JVM memory architecture to write the better code and maintain the application.We will discuss each component in detail.
Method Area-It stores each class structure such as static methods,static variables,instance method,run time constant pool,static initializers, constructor.It is created on JVM startup.If memory allocated in method area is not sufficient then OutOfMemory error is thrown by JVM. The method area may be of fixed size or dynamic .
Heap Space- Object or Instance Variable acquires space in heap memory.Heap memory is shared among all the JVM threads.Heap space is managed by automatic garbage collection.If the program requires more space than space managed by Garbage Collector then OutOfMemory error is thrown by JVM. The heap memory may be of fixed size or dynamic .
Let take an example to understand memory allocation:
class Employee{
int employeeId;
double employeeSalary;
}
Create Employee Object;
Employee emp=new Employee();
When employee instance created the base size allocated will be 8bytes.
4 bytes - class reference
4 bytes - object lock status
Employee class has two variable with datatype int and double.
For employeeId - 4bytes
For employeeSalary - 8bytes
Totals Space = 4+4+4+8=20 bytes
However,JAVA uses padding of 8 in memory allocation,so total 24bytes.
Thus the conclusion is that object creation consume a lot of memory so we have to be very careful while creating a new object.
Stack-Java methods invoked and a local variable is stored in the stack.This memory is used for execution of a thread.Stack memory size can be fixed or dynamic.
If thread requires more space than allocated for storing method call or local variable then java.lang.stackOverflowError.
In the case of dynamic memory allocation when stack tries to acquire more space and space is not available the JVM throws java.lang.OutOfMemoryError.
PC Register-It keeps the track of program execution. JVM supports multiple thread and each thread has its own PC Register.At a time each thread execute single method i.e. current method for thread.It method is native PC register store address of JVM statement being executed.If method is non native then PC register is not created.
Native Method Stack-This space is used by native method.If native method is not present then this stack does not get created.Native method stack memory size can be fixed or dynamic.
If thread requires more Native Method Stack space than allocated then java.lang.stackOverflowError.
In case of dynamic memory allocation when stack tries to acquire more space and space is not available the JVM throws java.lang.OutOfMemoryError.
Hope you like this tutorial.
You may be interested in Java Basic Tutorial.
Reference- https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html
Give you suggestion in comments.If you like the post Share with your friends on social network.
details on JVM memory allocation.It is very important for the developer to understand the JVM memory architecture to write the better code and maintain the application.We will discuss each component in detail.
Method Area-It stores each class structure such as static methods,static variables,instance method,run time constant pool,static initializers, constructor.It is created on JVM startup.If memory allocated in method area is not sufficient then OutOfMemory error is thrown by JVM. The method area may be of fixed size or dynamic .
Heap Space- Object or Instance Variable acquires space in heap memory.Heap memory is shared among all the JVM threads.Heap space is managed by automatic garbage collection.If the program requires more space than space managed by Garbage Collector then OutOfMemory error is thrown by JVM. The heap memory may be of fixed size or dynamic .
Let take an example to understand memory allocation:
class Employee{
int employeeId;
double employeeSalary;
}
Create Employee Object;
Employee emp=new Employee();
When employee instance created the base size allocated will be 8bytes.
4 bytes - class reference
4 bytes - object lock status
Employee class has two variable with datatype int and double.
For employeeId - 4bytes
For employeeSalary - 8bytes
Totals Space = 4+4+4+8=20 bytes
However,JAVA uses padding of 8 in memory allocation,so total 24bytes.
Thus the conclusion is that object creation consume a lot of memory so we have to be very careful while creating a new object.
Stack-Java methods invoked and a local variable is stored in the stack.This memory is used for execution of a thread.Stack memory size can be fixed or dynamic.
If thread requires more space than allocated for storing method call or local variable then java.lang.stackOverflowError.
In the case of dynamic memory allocation when stack tries to acquire more space and space is not available the JVM throws java.lang.OutOfMemoryError.
PC Register-It keeps the track of program execution. JVM supports multiple thread and each thread has its own PC Register.At a time each thread execute single method i.e. current method for thread.It method is native PC register store address of JVM statement being executed.If method is non native then PC register is not created.
Native Method Stack-This space is used by native method.If native method is not present then this stack does not get created.Native method stack memory size can be fixed or dynamic.
If thread requires more Native Method Stack space than allocated then java.lang.stackOverflowError.
In case of dynamic memory allocation when stack tries to acquire more space and space is not available the JVM throws java.lang.OutOfMemoryError.
Fig 1.1- JVM Memory Architecture
You may be interested in Java Basic Tutorial.
Reference- https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html
Give you suggestion in comments.If you like the post Share with your friends on social network.
JVM Runtime Memory Allocation
Reviewed by JavaInstance
on
4:28:00 AM
Rating:
Reviewed by JavaInstance
on
4:28:00 AM
Rating:

No comments: