All Posts

Java Heap vs Stack

Heap vs Stack

Whenever a class is executed by specifying its corresponding class name using the command java {classname}€ the java launcher JAVA immediately initiates the Java run-time environment for the class execution as a layer on top of the operating system.

Java Heap

  • Main memory of JVM.
  • Objects of classes.
  • Non-static variables memory is created.
  • Method area and heap area both are memory sharable areas.

Java Stack

  • All java methods are executed.
  • JVM by defaults creates Main Thread and Garbage collector thread
  • Each method execution JVM creates a separate block in the main thread. This is called the Stack Frame.
  • Java operations are called Stack Frame operations because every method is executed only in the stack.

Stack Frame is a sub-block inside a thread created to execute a method or block and is destroyed automatically after the completion of the method. If the method has any local variable they all are created inside the method’€™s stack frame and are destroyed automatically when the stackframe is destroyed. Stack Frame is divided into 3 blocks:

  • Variables storage area:for method’€™s local variable
  • Instruction storage area: To store instruction information
  • Method stack:To execute method.

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/