JAVA VIRTUAL MACHINE

JAVA VIRTUAL MACHINE

Definition of Java Virtual Machine

JVM is responsible to run Java bytecode by translating them into current OS machine language. Moreover, JVM provides runtime environment with an equally partitioned identical memory area for executing java Bytecode in any OS.

The main reason behind the invention of JVM is to achieve platform independency, means JVM is invented for running java compiled code (bytecode) in all available OS irrespective of the OS in which it is compiled.

Background process : Running java command

We run java command, JVM is created as a process in OS by occupying some memory from RAM and JVM performs internally below three phases:-

  1. Class Loading phase:

  • Once JVM process is create, JVM will request Class loader to load the given class bytecode.
  • If class bytecode not available then Class Loader throw exception? ClassNotFoundException?/ ?NoClassDefFoundError?(both are same), from Java 8 onwards?Error: Could not find or load main class.

2. ByteCode verification phase:

  • After class loading phase, Bytecode verifier verifies the loaded Bytecode’s internal format. if the bytecode is in JVM understandable format, it allows interpreter to execute loaded bytecode.
  • If loaded bytecode is not in the JVM understandable format, it’ll terminate execution by throwing exception?java.lang.ClassFormatError.

3. Execution / Interpretation phase:

  • After bytecode verification completed, interpreter will executed loaded bytecode by converting them into current OS machine language.
  • While executing bytecode if any logical mistakes found, JVM will throw associated exception.

No alt text provided for this image

ClassLoader :

ClassLoader is a class it is responsible to perform below three activities

  • Loading :?In class loading phase ClassLoader will load & store given class bytecode into JVM’s Method area. ClassLoader subsystem contains three types of class loaders for loading classes.

  1. BootstrapClassLoader :?It is responsible to load java library classes from BootStrapClasspath. i.e.; from?jdk\jre\lib\rt.jar?file.
  2. EtensionClassLoader :?It is responsible to load user defined classes or java library classes from ExtensionClasspath, i.e.; from?jdk\jre\lib\ext?folder.

  • This class loader is implement in java with class name?ExtClassLoader.

  1. ApplicationClassLoader/SystemClassLoader :?It is responsible to load user defined classes from ApplicationClasspath. It’ll load classes from current working directory or from the folders configured in class path environment variable.

  • Linking :?Linking is the process of incorporating the binary type data into the runtime state of the virtual machine.

  1. Verification phase, Classloader will check the type is properly formate and fit for use by JVM.
  2. Preparation phase, static variables memory is allocate with default values based on their data type.
  3. Resolution phase, symbolic references in the constant pool are transfer into their direct references.

  • Initialization :?In initialisation phase, static blocks execute and the static variables are initialize with their initial values give in their declaration.

Runtime Data Areas :

  • Method Area :

  1. In Method area, every class bytecode is load and store. It means the given class static and instance variables declaration statements, blocks, methods, and constructors logic will be store in method area.
  2. JVM will throw?OutOfMemoryError?exception if there is not sufficient memory in method area.

  • Heap :

  1. It is like main memory of JVM, all classes and arrays objects are create in heap area. Means given class instance variables memory is allocate in heap area.
  2. JVM will throw?OutOfMemoryError?exception if there is not sufficient memory in heap area.

  • Java stacks :

  1. All methods(non-native), blocks and constructors logic is executed.
  2. we can create several threads for executing methods at a time concurrently. JVM by default creates minimum two threads

  • main thread
  • garbage collector thread

  1. When we invoke a method or constructor a new stack frame create. Once this method execution completed this stack frame is destroyed.
  2. Stack frame holds logic of the method, it’s parameters, local variables, its return values and intermediate calculations.

  • Program Counter Registers Area :

  1. PC Register is create when new thread is create, after thread execution complete it is destroy automatically.
  2. It is used for tracking execution in this thread by storing its instruction address.

  • Native Method Stack Area :

  1. In Native methods stack area, all native methods are execute.
  2. Native method is written in some other language, such as C, C++, or assembly, and compiled to the native machine code of a particular processor.
  3. to create native moths we must place native keyword in its prototype and it should not have body. A java program interact with the host OS by invoking native methods.

Execution Engine :

  1. It’s responsible to execute java bytecode.
  2. All executions happens in JVM are control by execution Engine.
  3. It uses interpreter, JIT compiler, Profiler, Security Manager for executing a program.
  4. It also contains garbage Collector for destroying unreference objects from Heap Area.
  5. JIT Compiiler will find the code which is executing repeatedly, generates machine language code only once, buffer it and reuses the same machine language code for repeated execution.

Java Native Interface (JNI) :

  • The Java Native Interface (JNI) enables Java code running in a Java virtual machine (JVM) to call and be invoke by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.

要查看或添加评论,请登录

Vivek Yadav的更多文章

  • HOW AWS IS HELPING COMPANIES?

    HOW AWS IS HELPING COMPANIES?

    What is AWS? AWS Meaning: The Amazon Web Services (AWS) platform provides more than 200 fully featured services from…

    1 条评论
  • Microservices Design principle & Design patterns

    Microservices Design principle & Design patterns

    What is Microservice Architecture? The microservice architecture is structured on the business domain and it's a…

  • MICROSERVICES AND ARCHITECTURE

    MICROSERVICES AND ARCHITECTURE

    What Are Microservices? Microservices are an architectural style that structure applications as a collection of loosely…

社区洞察

其他会员也浏览了