How can JVM manage memory in java?

Actually JVM takes some memory from operating system and that memory divided into two parts:  1. Heap Memory and 2. Stack Memory.
Here we are not going to discuss about Heap and Stack. Our focus is on the JVM memory management.
When you create an object for the class then following things will happen:

  •          Byte code in .class file will be loaded in main memory.
  •         JVM creates the default object for java.lang.class.
  •          JVM identifies all the methods in the class and then stack will be constructed for that class with ‘n’ number of stack frames where ‘n’ depending upon number of methods in class one method definition will stored in stack frames.
  • ·         JVM identifies the loaded class’s block and constructed the another stack and files the stack with all block.
  •          JVM allocate the memory for the static variable.
  •          JVM executes the static block of class.
  •          Memory will be allocated for the reference variable.
  •          Memory will allocated for the instance variable of the loaded class.
  •          JVM executes the instance block of the loaded class.

  •    Constructor will be called.
  •         JVM assign a newly created object address to reference variable.
  •          When you called method with created object, then JVM do the following:

1.      Identifies the stack frames where the method is stored.
2.      After identifying the stack frames, JVM starts executing all the statement one by one available.
3.      If any local variables declaration in the method, JVM allocates the memory for those local variables in the scratch memory of the frame whose the method is stored.
4.      After finishing the method execution memory allocated for the local variable will get deleted.
Please look at these points:
  All methods definition:---Stored in stack memory.
  All objects:----Stored in Heap memory.
Reference variable:--- Depends.
Local Variables:---Stored in stack memory.
Primitive variables:---Depends.
Static variables:---Heap Memory
Static block:---Stack memory.
Instance block:----Stack Memory.
Local block:----Stack frame.
Instance variables:----Heap Memory.

1 comment :

  1. what about final local variable ? where they stored in memory ?