What is JVM (Java virtual machine) in java?
- JVM is the virtual machine on which java code executes.
- JVM is responsible for converting byte code into machine specific code.
JVM (Java Virtual Machine) Architecture?
- JVM (Java Virtual Machine) consists of Class Loader Subsystem, Runtime Data Areas and Execution Engine.
1. Classloader Subsystem
- Classloader is a subsystem of JVM.
- Classloader is used to load class files.
- Classloader verifies the class file using byte code verifier. Class file will only be loaded if it is valid.
2. Runtime Data Areas
- Method Area
- Method area is also called class area.
- Method area stores data for each and every class like fields,constant pool,method’s data and information.
- Heap
- Heap is place where all objects are stored in JVM (java virtual machine).
- Heap even contains arrays because arrays are objects.
- Java Threads (Java thread Stacks)
- You must know that each and every thread has its own stack.
- How stack frames are created when thread calls new method?
- As we know each and every thread has its own stack. Whenever Thread.start() method is called new stack frame is created and it is pushed on top of that thread's stack.
- What does thread stack contains?
- The stack contain
- All the local variables
- All the parameters
- All the return address
- Does stack stores/contains object OR what stack doesn’t contains?
- Stack never stores object, but it stores object reference.
- Program counter registers (PC Registers)
- Program counter registers contains the address of instructions currently being executed and address of next instruction as well.
- Native internal Threads (Native thread stack )
- Native internal threads area contains all the informations related to native platform.
- Example - If we are running JVM (java application) on windows, it will contain all information related to native platform i.e. windows.
- If we are running JVM (java application) on linux, it will contain all information related to native platform i.e. linux.
3. Execution Engine
- Execution Engine contains JIT (Just In Time) Compiler and Garbage collector compiler. Execution Engine also contains Interpreter.
- JIT(Just In Time) compiler
- JIT compiler compiles bytecodes to machine code at run time and improves the performance of Java applications.
- JIT Compiler internal working
- JIT compilation does require processor time and memory usage. When the JVM first starts up, lots of methods are called. Compiling all of these methods might can affect startup time significantly, though program ultimately may achieve good performance.
- Methods are not compiled when they are called first time. For each and every method JVM maintains a call count, which is incremented every time the method is called.
- The methods are interpreted by JVM until call count not exceeds JIT compilation threshold (The JIT compilation threshold improves performance and helps the JVM to start quickly. The threshold has been selected carefully by java developers to obtain an optimal performances. Balance between startup times and long term performance is maintained).
- Therefore, very frequently used methods are compiled as soon as JVM has started, and less used methods are compiled later.
- How JIT improves performance of Most frequently used methods ?
- After a method is compiled, its call count is reset to zero and subsequent calls to the method increment it call count. When the call count of a method reaches a JIT recompilation threshold, the JIT compiler compiles method second time, applying more optimizations as compared to optimizations applied in previous compilation. This process is repeated until the maximum optimization level is reached. Most frequently used methods are always optimized to maximize the performance benefits of using the JIT compiler.
- Example
- Let’s say JIT recompilation threshold = 2
- After a method is compiled, its call count is reset to zero and subsequent calls to the method increment it call count. When the call count of a method reaches a 2 (i.e. JIT recompilation threshold), the JIT compiler compiles method second time, applying more optimizations as compared to optimizations applied in previous compilation.
- Garbage Collector
- Garbage Collector Garbage collection is the process by which JVM clears objects (unused objects) from heap to reclaim heap space.
- Interpreter
- Interpreter is responsible for reading the bytecode and then executing the instructions.
4. Native method libraries
- Native method interface is an interface that connects JVM with the native method libraries for executing native methods.
- Example of Native method libraries
- If we are running JVM (java application) on windows, then Native method interface(window method interface) will connect JVM with the window methods libraries(native method libraries) for executing window methods (native methods).
- You must know about JNI, What is Java Native Interface(JNI)?
- You may write your application purely in java but there are certain situations where java code might need meet your requirement.
- Programmers uses the JNI (Java Native Interface) to write the Java native methods when an application cannot be written purely in Java.
JVM components related to performance
- Three components Heap, JIT (Just In Time) Compiler and Garbage collector are related to JVM’s performance tuning.
1. Heap and Garbage collector for tuning JVM’s performance
- All the objects are stored in heap. Garbage collector manages the heap at JVM initialization.
- There are many VM (JVM) options for Increasing and decreasing the heap size for managing object for best performance.
- selecting the different garbage collector depending on your requirement.
2. JIT (Just In Time) Compiler for tuning JVM’s performance
- JIT compiler compiles bytecodes to machine code at run time and improves the performance of Java applications.
- In newer versions of JVM tuning of JIT (Just In Time) Compiler is rarely needed.
How is java platform independent language?
- Once source code (i.e. .java file) is compiled on one platform(bytecode is formed). That bytecode can be executed (interpreted) on any other platform running a JVM.
- Every platform have different JVM implementation.
[Core Java Interview Questions]
We recommend you take Big Data Hadoop class room training at eMexo Technologies in electronic city, Bangalore to learn more about Big Data Hadoop.
We recommend you take Big Data Hadoop class room training at eMexo Technologies in electronic city, Bangalore to learn more about Big Data Hadoop.
1 Comment:
Upgrade your skill to Big data Hadoop
https://www.emexotechnologies.com/courses/big-data-analytics-training/big-data-hadoop-training/
Post a Comment