JVM Memory Management in Java

JVM (Java Virtual Machine)

The JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java byte code can be executed. It allows a system to run a program. The three notions of JVM are specification, implementation and instance.

JVMs are available for many hardware and software platforms (i.e.JVM is platform dependent). We will learn more about JVM in this chapter.

What is JVM?

  • A specification where the working of Java Virtual Machine is specified, but the implementation provider is allowed to choose the algorithm independently. Its implementation has been provided by Sun micro system and other companies.

  • An implementation is known as JRE (Java Runtime Environment) is used here.

  • Runtime Instance : Whenever you write a java command on the command prompt to run the java class, an instance of JVM is created.

What does JVM do?

The JVM performs following operations:

  • Loads code

  • Verifies code

  • Executes code

  • Provides runtime environment

JVM provides definitions for the:

  • Memory area

  • Class file format

  • Register set

  • Garbage-collected heap

  • Fatal error reporting etc

Let's understand the internal architecture of JVM. It contains classloader, memory area, execution engine etc. Study the diagram below for more clarity:

internal architecture of jvm in java

  • Class loader :
    Class loader is a subsystem of JVM that is used to load class files.

  • Class (Method) Area :
    Class (Method) Area stores per-class structures, such as the runtime constant pool, field and method data, the code for methods.

  • Heap :
    It is the runtime data area in which objects are allocated.

  • Stack:
    Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return.
    Each thread has a private JVM stack, created at the same time as thread.
    A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

  • Program Counter Register:
    It contains the address of the Java virtual machine instruction currently being executed.

  • Native Method Stack:
    It contains all the native methods used in the application.

  • Execution Engine:
    It contains :

    • A virtual processor

    • Interpreter: Which reads byte code stream then executes the instructions

    • Just-In-Time (JIT) compiler: It is used to improve the performance of the program. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here, the term compiler refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

At JVM start up, JVM occupies the same memory from operating system to run the java programs. Objects in java are stored in what is called as the heap, which when full, activates the garbage collection. During the garbage collection process, only the objects which are no longer in use are cleared. This opens up more memory space.

To understand JVM memory better, you need to know that it is divided into two parts:

  1. Stack memory
    Stack memory is used for static memory allocation which stores local variables and function calls. It is a private memory and cannot be viewed by everyone.

  2. Heap memory
    Heap memory stores objects in Java and global.

Whenever you run the program, JVM allocates the memory for variables used in the program and objects created in the program. It is a public memory and can be viewed by anyone.

When JVM is loading class into main memory, the following tasks are performed:

  • Default objects will be created, which will be type java.lang.Class

  • Stacks will be constructed with all the block members of the class

  • Memory will be allocated for static variables of the class

  • Static blocks will be executed

When JVM is creating the object, the following tasks will happen:

  • Memory will be allocated for the reference variables

  • Memory will be allocated for the instance variables

  • Instance blocks will be executed

  • The appropriate constructor will be called

Newly created object address will be assigned to reference variables.

Call by value:

  • When you invoke a method by passing primitive as parameter, it is called call by value mechanism

  • In case call by value happens, modifications that have happened in the method will not be reflected to the caller of the method

Call by Reference:

  • When you invoke a method by passing object as parameters, it is called a call by reflected mechanism

  • In case of call by reference, modifications that have happened in the method will be reflected to the caller of the method

Q. Can I overload the main method?

Ans. Yes, you can overloaded it.

Q. Which main method will be called by JVM when a class contains multiple overload main methods?

Ans. JVM always calls the main method which is given in the following example.

Q. Can you call the main method inside main()?

Ans. Yes, you can.
You may call not only the main method, but any method inside the same method, which is called exclusive calling. Recursive calling must be terminated at the same point with the same condition otherwise you will get a stack overflow error.

Q. I have two classes called hello and hui, and both contain main methods. Which main() will be called?

Ans. The main () invocation will depend on the class name which you are passing command line.
For Example: When you write Java Hello- Main of Hello class will be called Java Hui- Main at Hui class will be called.

When you try to use the class, the JVM loads class into main memory.

We can use the class's code in two ways:
  • Creating the object for the class
  • Accessing static members with the class
When JVM is loading the class into main memory it will perform the following task:
  • Memory will be allocated for static variables
  • Static block will be executed
When you create an object, the task performed will be: Hello h=new Hello ();
  • Hello h : Memory will be allocated to the reference variables (static ariable)
  • New Hello : Memory will be allocated for the instance variables of the class (non-static variables)
  • Hello : Instance block will be executed
  • () : Appropriate constructor will be called
  • = : Newly created object's address will be assigned to the reference variable

Q. Can I declare the constructor as static?

Ans. NO → Error Modifier static is not allowed here.

Q. Can I access instance variables with the class name?

Ans. NO → Hello.a will only access static variables.
Error non-static variable cannot be referenced from a static can text.