JVM (Java) memory type | Types of memory in JVM (Java Virtual Machine)

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.
  1. Method Area
  2. Heap Area
  3. Stack Area
  4. PC Registers
  5. 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.
 
Method area memory type

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.
Heap area memory type


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:
Runtime r =  Runtime.getRuntime();
  • Once we got Runtime object we can call the following methods on that object.
 
Runtime example

Ex:

Memory program in java

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() )
Maximum heap size

  • -Xms - We can use this command to set minimum heap size.
Minimum heap size set


 


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 memory area in java

Stack frame structure

Each stack frame containes 3 parts
 
Stack memory area in java with stack frame structure


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.
Local Variable Array in java

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.
 
Operand Stack in java


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.
Native method stack in java
  • Static variables will be stored in Method Area.
  • Instance variable will be stored in Heap Area.  
  • Local variable will be stored in Stack Area

Ex:

Native method stack in java

 


RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post