Code to Memory mapping in Java

Map code to physical memory in Java

Operating System Perspective (OS) : From Operating System perspective, a process representation in memory is represented as

       

 

Take aways :

  • Operating System has the same perspective for all the processes, irrespective of which language has been used to create the binary for the process.
  • Its the same for JAVA , C , C++ , PHP, Perl , Python as its how the OS perceives the process .
  • The representation is on RAM , so what we call stack , heap , data or code is a logical binding , as underlying storage is RAM.
  • Stack is known to grow down with the criteria , one can add data onto the top and remove from the top.
  • Stack elements can be removed starting from top , no random deletion of elements are possible.
  • Heap is the logical structure where allocation and deallocation can happen randomly . (Heap is not queue )
  • Both stack and Heap differs only in the logical way of accessing the data (insertion , retrieval and deletion) and both are mapped on same Map.

[addToAppearHere]

What is so special about Java processes.

  • Java classes runs on JVM which internally manages HEAP.
  • The management guarantees that any allocation of memory on the JVM manged HEAP will be deallocated automatically.
  • To deallocate JVM runs garbage collection threads along with the Code you write.
  • All Java does is manages a part of the HEAP , you are free to allocate memory(JAVA provides API) outside the Java managed HEAP.
  • If one allocates memory outside JVM manged heap , deallocation is the responsibility of the programmer.

 

Lets take a sample Java class

public class SampleClass {

    private int state;

    public SampleClass(int input){
        state = input;
    }

    public int getState(){
        return  state;
    }

    public static void main(String[] args) {
        SampleClass var = new SampleClass(10);
        System.out.println(var.getState());
    }

}

 

[addToAppearHere]

Java code is compiled and executed , during code execution Java class files is consumed by JVM binary which generates instructions which are executed by CPU.

Most important instruction in code

SampleClass var = new SampleClass(10);

 Code Where in Process memory(RAM)
getState()  void main() HEAP area
SampleClass var STACK area
Sample Class has just one instance variable int a HEAP area

 

KEY TAKE AWAYS :

  • All the functions of a class is stored in Code area
  • The references are stored in stack
  • The actual class ( the instance variables ) are stored in Heap
  • Its not possible to instantiate Class on Stack in Java .
  • This is representation of Java code From OS process memory model .
  • All Java does is manages a part of the HEAP , you are free to allocate memory(JAVA provides API) outside the Java managed HEAP.
  • If one allocates memory outside JVM manged heap , deallocation is the responsibility of the programmer.

Lets take a deep dive in How JVM manages the memory .

 

Abstract view of JVM memory management.

JVM  specification provides flexibility on what to place where

 

 

 

 

 

Heap is divided into :   Young Generation |  OLD(Tenured) Generation |    Meta Generation (perm Gen in JDK 7 )

Classes  .class files META generation
static primitive type , refrences META Generation
object refrenced by Static refrences Tenured Generation
Functions inside the Class Method Area (can be in Meta or other part of heap)
local variables STACK
References or primitive as a instance variable HEAP

 

 

JDK 8 documentation