Dec 10, 2019

Understanding JVM: JVM Execution Engine

Now we know that how classes are loaded by Class Loaders, which are part of Class Loader Sub System, next thing that happens is the execution of those class files. JVM Execution Engine is the responsible component for this. It has two components:
  • Interpreter
  • JIT Compiler

Interpreter

  • It reads the byte code, interpret it into machine code (native code) and then execute them line by line.
  • Problem with interpreter is it interprets every-time even if same method invoked every-time which reduces performance of the system.
  • To overcome this problem, "Sun" introduced JIT Compiler in 1.1 version.

JIT Compiler

  • Main purpose of JIT Compiler is to improve performance. Internally it maintains a separate count for every method.
  • Whenever JVM comes across any method call, first that method will be interpreted normally by interpreter and JIT Compiler increments corresponding count variable.
  • This process is done for every method being executed.
  • Once any method reaches a threshold value, JIT Compiler identifies that this method is commonly used method. These commonly used methods are also called hot-spot.
  • Immediately JIT Compiler compiles that method and generates corresponding native code. Next time, for this method call, generated native code is used directly instead of interpreting it once again.
  • Threshold count varies from JVM to JVM.
  • Some advanced JIT Compilers recompile generated native code, if count reaches threshold value second time, so that more optimized machine code is generated.
  • Internally, profiler, which is part of JIT Compiler is responsible to identify hot-spots.
  • One thing to note here is that JVM interprets whole program at least once. JIT compilation is done only for repeatedly used methods, not every method.

With that, we are done with the series for JVM understanding. Please let me know your view and if you want any other topic to be covered.

Oct 25, 2019

How to get heap dump of Java Application?

There is a dedicated space where all the objects are stored at runtime. It is called heap. It is created at the time of JVM startup and is configurable using Xms (to be allocated at the time of JVM startup) and Xmx (maximum space to be sued heap) JVM options.

The JVM automatically performs Garbage Collection (GC) when it detects that the JVM is about to reach the max heap size. But the GC can only clean the objects which are eligible for GC. If the JVM can't allocate required memory even after GC, JVM will crash with "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space".
If your Java application in production crashes due to some issue like this, you can't just ignore the incident and restart your application. You have to analyze the what caused the JVM to crash, and take the necessary actions to avoid it happening again. This is where the JVM heap dump comes into play.
If JVM heap dumps are by default disabled, you have to enable heap dumps explicitly by providing the following JVM option: -XX:+HeapDumpOnOutOfMemoryError.
The below sample code, tries to create a multiple, large arrays of chars, and keep the references in list. Which cause those large arrays ineligible for garbage collection.

package com.test;
import java.util.ArrayList;
import java.util.List;
public class TestClass {
    public static void main(String[] args) {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < 1000; i++) {
            list.add(new char[1000000]);
        }
    }
}

If you run the above code with the following command lines:
1. java -XX:+HeapDumpOnOutOfMemoryError -Xms10m -Xmx3g com.test.TestClass
Result: Program runs and exits without any error. The heap size starts from 10MB and then grows as needed. Above program needs memory less than 3GB. So, it completes without any error.
2. java -XX:+HeapDumpOnOutOfMemoryError -Xms10m -Xmx1g com.test.TestClass
Result: JVM crashes with OOM.
If we change the above code a bit to remove the char array from the list, after adding to the list, this would be the result:

package com.test;
import java.util.ArrayList;
import java.util.List;
public class TestClass {
    public static void main(String[] args) {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < 1000; i++) {
            list.add(new char[1000000]);
            list.remove(0);
        }
    }
}

3. java -XX:+HeapDumpOnOutOfMemoryError -Xms10m -Xmx10m com.test.TestClass
Result: This code runs without any issue even with a heap of 10MBs.

Nov 15, 2017

Understanding JVM: Memory Areas in JVM - Part 3

We have already covered Method Area, Heap Area and Stack memory in previous articles. This article will give brief overview of the remaining 2 memory areas in JVM, i.e., PC Registers and Native Method Stack.

PC Register (Program Counter Registers)

  • It is internally used by JVM and like Stack Memory, every thread keeps a separate PC register, which is created at the time of thread creation.
  • It contains the address of currently executing instruction.
  • Once instruction execution completes, automatically PC register is incremented to hold the address of next instruction.

Native Method Stack

  • JVM creates a separate Native Method Stack per thread.
  • Again, it is internally used by JVM. All native calls invoked by thread will be stored in corresponding native method stack.

This concludes the topic of Memory Areas inside JVM. To summarize things:
  1. Method area, heap area and stack area are considered most important with respect to programmers.
  2. Method and heap area are per JVM.
  3. Stack memory, PC registers and Native Method stack are maintained per thread.
  4. Static variables are stored in Memory Area
  5. Instance variables are stored in Heap Area
  6. Local variables are stored in Stack Area

Let us write a simple program to understand things better: 

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package edu.javaterritory.example.methodarea;

public class Test {

 private Student s1 = new Student();
 private static Student s2 = new Student();
 
 public static void main(String[] args) {
  Test t1 = new Test();
  Student s3 = new Student();
 }
}

Following diagram shows where variables created in above program will be stored.

  • Static variables inside method area
  • Instance variables in heap area
  • Local variables in static memory area

So, that's all about method area's inside JVM. Hopefully it helps.
We will continue to remaining components inside JVM, i.e., Execution Engine and JNI (Java Native Interface).

Please provide your inputs in the comments section.
Also, you can follow us at +Java Territory (or here).

Nov 5, 2017

Understanding JVM: Memory Areas in JVM - Part 2

Stack memory

  • It is maintained per thread, i.e., for every thread in Java, JVM will create a separate stack at the time of thread creation.
  • Each and every method call performed by the thread will be stored in stack including local variable also.
  • After completing a method, the corresponding entry from the stack will also be removed. Thus, once all method calls are completed, stack memory will become empty.
  • This empty stack will be destroyed by JVM before terminating the thread.
  • Data stored inside stack is private to corresponding thread only and hence thread-safe.
  • Each entry inside stack is called stack frame/activation record.

Stack Frame structure

Each Stack Frame has 3 parts:
  1. Local Variable Array,
  2. Operand Stack, and
  3. Frame Data
    Local Variable Array
    • It contains all parameters and local variables of the memory.
    • Each slot in the array is of 4 bytes.
    • Type
      • int, float, object -> 1 slot (4 bytes)
      • double, long -> 2 slots (8 bytes)
      • byte, short, char -> converted to int before storing -> 1 slot (4 bytes)
      • boolean -> JVM dependent -> generally 1 slot
    Operand Stack
    • It stores intermediate values.
    • JVM uses operand stack as workspace.
    • Some instructions can push values to operand stack, some instructions can pop values, and some can performance required operations.
    Lets take an example to understand the process. Suppose a program adds 2 values, say 40 and 60.
    JVM instructions will look something like:
    iload - 0 // loads 40
    iload - 1 // loads 60
    iadd // adds 40 and 60
    istore -2 // store the result

    Following is the state if Local Variable Array after first 2 load instructions.
    Local Variable Array

    Values are also loaded in Operand Stack for addition. 
    Operand Stack


    Finally, sum is  pop out from the operand stack and put into local variable array.
    Local Variable Array

    Finally the third component in the frame is Frame Data.

    Frame Data

    • It contains all symbolic references related to the method.
    • Contains references to exception table and will provide corresponding catch block information in case of exception.
    This is all about Stack Memory. Hopefully, it helps.
    Next section will cover the other 2 memory areas, PC registers and native method stack.

    If you like reading our articles, do follow at +Java Territory / Java Territory.

    Oct 11, 2017

    Understanding JVM: Memory Areas in JVM - Part 1

    Whenever JVM load and run a Java program, it needs memory to store several things like bytecode, objects, variables, etc. Total JVM memory is organized into following:

    • Method Area
    • Heap Area
    • Stack Area
    • PC Registers
    • Native Method Stack

    1. Method Area

    • It is created at the time of JVM startup.
    • .class file's binary information is stored into method area.
    • Static variables are stored in method area.
    • Data inside method area is not thread safe.
    • Method area is not contiguous memory.
    • For every JVM, one method area is available.
    • Constant pools of a class are store inside method.

    2. Heap Area

    • There is one heap area per JVM.
    • Like method area, it is also created at the time of JVM startup.
    • Need not be contiguous memory.
    • Objects and corresponding instance variables are stored in the heap area.
    • Every array in Java is object only, hence arrays will also be stored in heap area.
    • Heap area can be accessed by multiple threads, and hence data in heap area is not thread safe.
    Program to Display heap memory statistics
    Heap memory statistics can be captured using Runtime instance. Runtime class is present in java.lang package. It is a singleton class.
    Following code snippet demonstrate how Runtime object can be obtained and used to get heap statistics:

    package com.javaterritory;
    
    public class MemoryTest {
    
     public static void main(String[] args) {
      Runtime r = Runtime.getRuntime(); // get instance of Runtime
      System.out.println("Max memory: " + r.maxMemory()); // prints memory (number of bytes) allocaed to JVM
      System.out.println("Total memory: " + r.totalMemory()); // prints initial memory (in bytes) allocated to JVM
      System.out.println("Free memory: " + r.freeMemory()); // prints the free memory 
     }
    }
    

    Following were the results when I executed it on my machine:

    Max memory: 3797417984
    Total memory: 257425408
    Free memory: 254741016
    

    How to set maximum and minimum heap size
    Following JVM arguments can be used to configuring heap size;
    -Xmx 512m : to set maximum heap size to 512 mb
    -Xms 64m : to set initial heap size to 64 mb

    This is it for Method and Heap area. Next section will cover the stack memory area.
    If you like reading our article, connect with us at +Java Territory  / Java territory to stay updated.