JVM Architecture

一、JVM Architecture

In the real world, JVM is a specification that provides a runtime environment in which Java bytecode can be executed. Different vendors provide different implementations of this specification. Most popular implementation of JVM is Hotspot which is owned and provided by Oracle Corporation.
The JVM is called virtual because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This independence from hardware and the operating system is a cornerstone of the write-once-run-anywhere value of Java programs.
JVM architecture diagram:
在这里插入图片描述

  1. ClassLoader
    The class loader is a subsystem used for loading class files. It performs three major functions i.e. class loading, linking, and initialization.

    Loading:
    To load classes, JVM has 3 kind of class loaders. Bootstrap, extension and application class loader.
    Bootstrap ClassLoader – Responsible for loading classes from the bootstrap classpath. It loads core java API classes present in JAVA_HOME/jre/lib directory. Highest priority will be given to this loader;
    Extension ClassLoader – It is child of bootstrap class loader. It loads the classes present in the extensions directories JAVA_HOME/jre/lib/ext(Extension path) or any other directory specified by the java.ext.dirs system property;
    Application ClassLoader –It is child of extension class loader. It is responsible to load classes from application class path. It internally uses Environment Variable which mapped to java.class.path.

    Loading process:

    • When loading a class file, JVM finds out a dependency for some arbitrary class XYZ.class.
    • First bootstrap class loader tries to find the class. It scans the rt.jar file in JRE lib folder.
    • If class is not found then extension class loader searches the class file in inside jre\lib\ext folder.
    • Again if class is not found then application classloader searches all the Jar files and classes in CLASSPATH environment variable of system.
    • If class is found by any loader then class is loaded by class loader; else ClassNotFoundException is thrown.

    Linking:
    Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get the verification error.
    Prepare – For all static variables memory will be allocated and assigned with default values.
    Resolve – All symbolic memory references are replaced with the original references from Method Area.

    Initialization:
    This is the final phase of class loading, here all static variable will be assigned with the original values and the static blocks will be executed.

  2. JVM Memory Areas

    1. Method Area(MetaSpace/PermGenSpace)
      JVM Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.When first time running a Java application, classloader will tries to load the classes by using the fully qualified name of each classes. Classloader will read the class file as stream of binary data and store the details inside method area. Method area is also responsible for allocating the memory to store the class / static variables. Method area is common for all the threads, so it must be designed to be thread safe.
      The memory of the method area could be allocated on JVM’s own heap. It is up to the implementation decision and not mandated by the specification. The method area also can be garbage collected. If a class becomes unreferenced, a JVM can unload the class to keep the memory occupied by the method area at a minimum.

    2. Heap
      All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads.
      If your application creating new objects or using arrays, then all the instances are stored in the heap memory of the Java Virtual Machine (JVM). Heap is the primary storage inside JVM for storing the runtime data that are accessed by the multiple threads. Heap memory is the common for all the threads, the data stored in the heap is accessible to all the threads running on JVM.
      Heap memory has the two logical portion based on the lifetime of every objects. First one is Young Generation and second one is Old Generation.
      If the total allocated memory is not sufficient for storing the new objects, then JVM will throw a java.lang.OutOfMemoryError. Application developers can customize the memory allocation for heap inside JVM by passing the following parameters:

    3. JVM language Stacks
      Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created simultaneously as the thread is created. When ever a thread enters a new method, a new block called as stack frame will be created inside stack memory for storing the method specific details. When the thread completes the execution of that method, corresponding stack frame will be pushed out of the stack memory. The total memory allocated for stack is very less compared to the heap memory.
      If the current stack memory is not enough for holding the method data (this happens if the method have recursive operation), JVM will throw java.lang.StackOverflowError error. If you are getting this error, you have to increase the stack memory or analyse the program whether it uses any recursive calls by mistake. Deep recursions also can cause StackOverflow errors.

      The Stack Frame is divided into three subentities:
      Local Variable Array – Related to the method how many local variables are involved and the corresponding values will be stored here.
      Operand stack – If any intermediate operation is required to perform, operand stack acts as runtime workspace to perform the operation.
      Frame data – All symbols corresponding to the method is stored here. In the case of any exception, the catch block information will be maintained in the frame data.

    4. PC Registers
      PC register store the address of the Java virtual machine instruction which is currently executing. In Java, each thread has its separate PC register.Program counter keeps a pointer to the current statements that is being executed in the current thread. If the current executing method is a native method, the counter register will be undefined.

    5. Native Method Stacks
      Native method stacks hold the instruction of native code depends on the native library. It is written in another language instead of Java.

  3. Execution Engine
    All code assigned to JVM is executed by an execution engine. The execution engine reads the byte code and executes one by one. It uses two inbuilt interpreter and JIT compiler to convert the bytecode to machine code and execute it.
    With JVM, both interpreter and compiler produce native code. The difference is in how they generate the native code, how optimized it is as well how costly the optimization is.

    • Interpreter
      This component reads the bytecode instructions and executes them in a sequential manner.
      A JVM interpreter pretty much converts each byte-code instruction to corresponding native instruction by looking up a predefined JVM-instruction to machine instruction mapping. It directly executes the bytecode and does not perform any optimization.

    • JIT Compiler
      This component counterbalance the Interpreter’s disadvantage of slow execution and improves the performance. JIT compiler compiles the similar part of the bytecode at the same time and thus reduces the total time needed for compilation.
      To improve performance, JIT compilers interact with the JVM at runtime and compile appropriate bytecode sequences into native machine code. Typically, JIT compiler takes a block of code (not one statement at a time as interpreter), optimize the code and then translate it to optimized machine code.

      The JIT compiler is enabled by default. You can disable the JIT compiler, in which case the entire Java program will be interpreted. Disabling the JIT compiler is not recommended except to diagnose or work around JIT compilation problems.

    • Garbage Collection
      This component is a part of execution engine which frees up the memory by collecting and removing the unreferenced objects

  4. Native Method interface
    The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.

  5. Native Method Libraries
    Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the Execution Engine.

二、Java code Compilation and Execution in Java VM

Note that there basically are 2 phases – Compile time and Run Time:
在这里插入图片描述

  1. We write the Java source code in abc.Java file using an editor or IDE (integrated development environment) e.g. Eclipse or IntelliJ Idea.
  2. Program has to be compiled into bytecode. Java compiler (javac) compiles the sourcecode to abc.class file.The compiler checks the code for syntax errors and any other compile time errors and if no error is found the compiler converts the java code into an intermediate code(abc.class file) known as bytecode. This intermediate code is platform independent
  3. This is the start of the Run Time phase, where the bytecode is loaded into the JVM by the class loader(another inbuilt program inside the JVM).
  4. Now the bytecode verifier(an inbuilt program inside the JVM) checks the bytecode for its integrity and if not issues are found passes it to the interpreter.
  5. Since java is both compiled and interpretted language, now the interpreter inside the JVM converts each line of the bytecode into executable machine code and passed it to the OS/Hardware i.e. the CPU to execute.
三、Why is Java slow?

The two main reasons behind the slowness of Java are

  1. Dynamic Linking: Unlike C, linking is done at run-time, every time
    the program is run in Java.
  2. Run-time Interpreter: The conversion of byte code into native
    machine code is done at run-time in Java which furthers slows down
    the speed

However, the latest version of Java has addressed the performance bottlenecks to a great extent.

参考:
《Java Virtual Machine (JVM) & its Architecture》
《Java Memory Management》
《Java (JVM) Memory Model – Memory Management in Java》
《Java Garbage Collection Basics》
《Java Version Upgrades: GC Overview》
《PermGen Elimination project is promoting》
《The JVM Architecture Explained》
《Execution Process of Java Program in Detail | Working of JUST-IT-TIME Compiler (JIT) in Detail》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值