JVM architecture and how it works

1 article/video left!

log in or sign up to unlock 3 more articles/videos this month and explore our expert resources.

Automatic Summary

Understanding JVM Architecture and its Working

In this article, we'll take a close look at the core of Java existence - the Java Virtual Machine (JVM). We'll explore the functionality of JVM along with its various components, including the class loader, JVM memory area, heap, and JVM language, among others.

My name is Kla Santos, a backend developer at M digital, Microsoft EVP, developer technologist, and book co-author. I have been actively organizing several tech communities such as We Makers Code and Pera Code.

Overview of JVM

JVM is the foundation of java applications and several other languages, like Clojure and Groovy, which also leverage JVM. With the philosophy of "write once, run anywhere," JVM allows code written on one machine to run seamlessly on another that uses JVM.

JVM works by compiling Java code to bytecode, which is then interpreted on different machines. Bytecode is an intermediary language acting as an interface between the host systems and the Java Source.

Components of JVM

The Class Loader

The class loader is integral to the JVM and performs three key operations i.e., loading, linking, and initialization. When a Java file is compiled, it is converted into a bytecode (.class file). The class loader loads this class into the main memory for the application to utilize. It includes three built-in class loaders - bootstrap class loader, extension class loader, and the application class loader.

JVM Memory

JVM memory comprises five components - method area, heap, JVM language stack, program counter register, and native method stack. These are crucial for storing class-level data, maintaining objects, trend creation, storing current execution instructions, and storing native methods respectively.

The Execution Engine

The execution engine consists of the interpreter, Just-In-Time (JIT) compiler, and the garbage collector. The interpreter reads the bytecode line-by-line, whereas the JIT compiler optimizes repeated code lines into native code, improving performance. The garbage collector, on the other hand, automates the memory management by collecting and removing unreferenced objects from the heap memory.

Native Method Interface and Native Method Libraries

The native method interface acts as a bridge interacting with the native method libraries and aids the JVM in calling or being called by C and C++ libraries using the native keyword. Native method libraries are written in other programming languages such as assembly, C and C++.

Java Compilation and Execution

The last part of understanding JVM requires insight into the process of Java compilation and execution. Java files are first compiled into .class files which consist of bytecode. Then, the JVM brings these class files into memory using the class loader and verifies the bytecode for security breaches. Finally, the JVM converts bytecode into machine code using JIT compiling.

Conclusion

In essence, JVM forms the crux of Java programming and underpins its running. Understanding its architecture and working not only equips you with a deep understanding of how Java operates under the hood but also enables you to debug and optimize your Java applications more effectively.

If you have any questions or wish to connect, feel free to reach out to me on LinkedIn or any other social networks. I hope this article helps you in understanding JVM and its significant components.


Video Transcription

Read More