Types of memory in JVM (Java Virtual Machine)
When ever JVM loads and run a java program it needs memory to store several things, like bytecodes, objects, variables etc.
Total JVM memory organized into following 5 categories.
Total JVM memory organized into following 5 categories.
- Method Area
- Heap Area
- Stack Area
- PC Registers
- Native method Area
1. Method Area:-
- For every JVM one method area will be available.
- Method area will be created at the time of JVM startup.
- Inside method area class level binary data including static variables will be stored.
- Constant pool of a class will be stored inside method area.
- Method area can be accessed by multiple threads simultaneously. So it is not thread safe.
2. Heap Area:-
- For every JVM one Heap area will be available.
- Heap area will be created at the time of JVM startup.
- Objects and corresponding instance variable will be stored in the heap area.
- Every array in java is object only. Hence arrays also will be stored in the heap area.
- Heap area can be accessed by multiple threads and hence the data stored in the heap memory is not thread safe.
- Heap area need not be continueous.
Note:
- A java application can communicate with JVM by using Runtime object.
- Runtime class present in java.lang package and it's a singleton class.
- We can create Runtime object as follow:
Ex:
How to set max and min heap sizes?
Heap memory is finite memory, but based on our requirement we can set min and max heap sizes.
i.e. we can increase or decrease the heap size based on requirement.
We can use the following flag with java command.
- -Xmx - To set maximum heap size ( maxmemory() )
3. Stack Area:-
- For every thread JVM will create a separate stack at the time of thread creation.
- Each and every method call performed by that thread will be stored in the stack. including local variables also.
- After completing a method the corresponding entry from the stack will be removed.
- After completing all method call the stack will become empty and that empty stack will be destroy by the JVM just before terminates the thread.
- Each entry in the stack is called Stack frame/ Activation record.
- The data store in the stack available for the corresponding thread and not available to the remaining thread, hence this data is thread safe.
Stack frame structure
Each stack frame containes 3 parts
A. Local Variable Array:
- It contain all parameters and local variables of the method.
- Each slot in the array is of 4 bytes values of the type - int, float and reference occupy one entry in the array.
- Value of double and long occupy 2 consecutive entries in the array.
- byte, short and char value will be covered into int type before storing and occupy one slot.
- But way of storing boolean value is varied from JVM to JVM but most of JVM follow one slot for this.
B. Operand Stack:
- JVM use operand stack as work space. Some instructions can push the value to the operand stack and some instruction can pop value from operand stuck and some instruction can perform required operations.
C. Frame Data:
- It contain all symbolic references realted to that method.
- It also contain reference to exception tables with provide corresponding catch block information in the case of exceptions.
4. PC Register (Program Counter Register):-
- For every thread a separate PC Register will be created at the time of thread creation.
- PC Register contains the address of current executing instruction.
- once instruction execution completes, automatically PC register will be increment to hold address of next instruction.
5. Native Method Stack:-
- For every thread JVM will create a separate native method stack.
- All native methods call invoked by the thread will be stored corresponding native method stack.
Notes:
- Method Area, Heap Area and Stack Area are considered as important memory area with respect to programmer.
- Method Area and Heap Area are per JVM where as Stack Area, PC Register and Native Method Stack are per thread.
- Static variables will be stored in Method Area.
- Instance variable will be stored in Heap Area.
- Local variable will be stored in Stack Area.