What all memory areas are allocated by JVM?

Being an abstract machine, JVM (Java Virtual Machine) as a program/software takes Java bytecode and converts the byte code (line by line) into machine understandable code. To run Java applications, it acts as a run-time engine. Along with being a part of the JRE(Java Runtime Environment), JVM is also the one that calls for the main method present in Java code.

Some particular types of operations are performed by JVM. These are:

  • Loading of code.
  • Verification of code.
  • Executing the code.
  • Providing a run-time environment to the users.

Types of Memory areas allocated by the JVM:

Different functions can take different forms of memory structure. In JVM, the memory can be divided into 5 different parts:

  • Class(Method) Area
  • Heap
  • Stack
  • Program Counter Register
  • Native Method Stack
  • Class Loader:

As a subsystem of JVM, the Class Loader is used to load class files and is principally responsible for the below activities.

  • Loading
  • Linking
  • Initialization
  • Class(Method) Area:

The class-level data of every class is stored by the Class(Method) Area. This class level data can be a runtime constant pool, field and method data, or the code for methods.

Heap:

To allocate memory to objects at run time, Heap is used.

Stack:

The private JVM stack that each thread contains, is created at the same time as thread and is used to store data and partial results. These data and results are needed while returning value for method and performing dynamic linking. A new frame is created each time at every invocation of the method and is destroyed when its method invocation completes. The Java Stack stores these frames.

Program Counter Register:

A program counter register is linked with each JVM thread that carries out the task of a specific method. The PC of the non-native method stores the address of the available JVM instruction. The value of the program counter, in a native method, however, is undefined. The ability to store the return address or a native pointer on some specific platform is what a PC register have.

Native method Stacks:

Native method stacks are also called C stacks. They are not written in the Java language. For each thread, this memory is allocated when it is created. This memory can either be of a fixed or dynamic nature.