Updated March 14, 2023
Introduction to JVM
Java Virtual Machine (JVM) is a powerful engine that offers a Java Software or application runtime environment. Java bytecode is converted into the language of your machine. JVM’s a Java Run Environmental (JRE) portion. The compiler produces machine code for a particular system in other programming languages. Bytecode is an intermediate language between the host and the Java source.
Understanding
Java is known as platform-independent due to the Java Virtual Machine. When a submission is done into different computers with JVM’s different operating systems, the bytecode is interpreted into machine-level language by JVM. To be precise, JVM programs are written in C Programming Language, while JVM is independent of the operating system. Therefore, it is a key component of Java Architecture. Apart from all this, it is responsible for assigning the essential memory required by a Java program. It is also responsible for prohibiting memory space.
What does it do?
The crucial operations which JVM performs are as follows:
- Code Loading
- Code Verifying
- Code Execution
- Runtime Environment Provision
Apart from all this, it rolls out classifications for Class file format, Memory area, Garbage-collected heap, Register set, Fatal error reporting, etc.
JVM Architecture
Given below are some key insights about the internal architecture of JVM, which comprises of:
1. Classloader
It is a subsystem of JVM used to load class files. While running the java program, it first gets loaded by the classloader.
The three classloaders which Java includes are as follows:
- Bootstrap ClassLoader: It’s the superclass of Extension classloader. It assists in loading the rt.jar files that carry all the class files.
- Extension ClassLoader: It is the parent classloader of System classloader and child classloader of Bootstrap. The jar files inside the $JAVA_HOME/jre/lib/ext directory can be extracted through the Extension ClassLoader.
- System/Application ClassLoader: It is the child of the Extension classloader, also known as Application classloader. The class files from the classpath can be extracted through it. The Classpath, which is set to the current directory, can be changed by default using the ‘-cp’ or ‘-classpath’ switch.
2. Class (Method) Area
The pre-class structures like field and method data, runtime constant pool, and the code for methods are stored in the Class (Method) Area. It is a memory that is created on JVM startup and is shared among all Threads like Heap.
3. Heap
The objects are assigned through this useful runtime data area. It is a memory place storing objects along with its instance variables. Whenever an object is created in Java, it goes into the heap – the area of memory.
4. Stack
Along with storing frames, Java Stack holds variables and partial results. It also plays a pivotal role in method, invocation, and return. Every individual thread consists of its private JVM stack, created at the same time as the thread. Whenever a method escalates, a new frame is generated and shattered when the method invokes.
5. Program Counter (PC) Register
The PC register contains the JVM instruction address at the time of its execution. Thus, keeping track of instructions has value for every instruction, which is undefined for the native methods.
6. Native Method Stack
The native procedures used in the applications are part of the Native Method Stack. These methods are written in languages other than Java. Associated with each thread, JVM implementations can’t rely on conventional stacks and cannot load native methods. To be precise, it is similar to stack but used for native methods.
7. Engine
Being the key aspect of JVM, it communicates with its various memory areas.
- Garbage Collector: This plays a vital role in the JVM’s dynamic memory management system. The memory which is occupied and no longer referenced by any of the reachable Java objects is released through the Garbage Collector.
- Interpreter: It executes the instructions after reading the bytecode stream.
- Just-In-Time (JIT) compiler: Enabling performance enhancement, JIT compiler compiles parts of the bytecode with similar functionality at the same time. It helps in reducing the amount of time needed for the compilation process.
8. Java Native Interface (JNI)
The applications scripted in other languages, i.e. C, C++, Assembly, etc., can be connected to JNI’s interface. JNI framework is used by Java to interact with OS libraries or to send output to the Console.
What is the use of JVM?
The JVM has two primary functions: to allow Java programs to run on any device or operating system (known as the “Write once, run anywhere” principle) and to manage and optimize program memory.
Advantages and Disadvantages
Java Programs are aptly executed through JVM. Bytecode – an intermediate language is used to compile Java programs and then executed through JVM. Any platform fueled by JVM can perform every individual program which is being compiled into bytecode. This enables Java software more effective as well as compatible as per the various computing platforms are concerned. However, the JVM has a few notable advantages and disadvantages that any developer should weigh carefully before investing in Java development.
1. Security
Through JVM’s security knacks, the programmers can effectively write high-end secured Java programs. Furthermore, it empowers the Operating System resources to identify the malicious software and also helps in its prevention accordingly.
2. Performance
Java programs that run on a JVM are likely to offer slower execution than the programs written in C++. This is due to the code optimization, which excessively relies on different system-specific features. Also, the Java bytecode cannot be optimized for a specific hardware set, as it is system-neutral.
3. Correctness
Correct is the program, which performs effectively and meets a user’s expectation to the fullest. JVM’s in-built features enable it to operate correctly and free of errors.
Why should we use JVM?
In spite of having Java syntactical code, JVM can run on various different language programs.
A few of the reasons why it is so widely used and why to use it across different platforms are listed below:
- It uses heap memory: Heap memory is a core component used for dynamic memory allocation for defining classes and object initialization. Since JVM supports heap memory, it has wide acceptance.
- Providing security for remote code location: The JVM framework is designed so that remote application is easily executed. This theory is followed for executing the Java Applet.
Conclusion
The fact that JVM works on multiple operating systems gives it a thumbs-up in the technology domain. Using it on Windows, Linux, and other OS’s, it is enabled to transform bytecode regardless of the hardware and the OS that is to be executed. In addition, its ability to offer security to the host computer by securing their data and program is another reason why JVM has been so successful in the past and continues to be dominant in the future.
Recommended Articles
This has been a guide to What is JVM? Here we discuss the basic concepts with the list of architecture and uses of JVM respectively. You can also go through our other suggested articles to learn more –