JVM architecture and how it works

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

Today we talk about JCM architecture and how it works. First uh who I am, I am Kla Santos, actually I work as a backend developer at M digital. I am Microsoft EVP and developer technologist.I am also book co author from the book Journal de Java is a Portuguese book and I am com community organizer of some tech units, some as we makers code pera code. And the uh let's talk this we cover in this presentation. Uh We talk about what is JVM and how it works, class loader JVM, me area heap and JVM language, text program counter hash three nature method, text execution giant interpreter just in time compiler, garbage collector and nature of a method interfaces and java compilation and execution. Yeah.

First uh what is JVM? Uh JVM is the core of java existence and a basic applications and other languages that also use JVM such as closure uh Cole groove and others. And to follow the FRA right ones room anywhere with JVM code, writing it on, on one machine can be run on another that use the JVM. And then in the simplest way how JA uh JVM works. Java code is compiled to bytecode is interpreted on different machines. And bytecode is an intermediary language between host systems and java Source. Here we have an illustration about JFM architecture. We cover out stop, we'll cover class loader JFM memory method area. Hi PJFM language, text PC, native methods, text execution, DGN method interface and native method libraries.

First, let's talk about class loader. Class loader is responsible for three shifts into JVM and loading linking and initialization. Uh In class loader. When you compile a Java file, it is converted into a bytecode a dot class file. And to use this class in our application, the class loader loads each into the main memory in the loading uh process. Usually the first class to be loaded into memories. The main class that contains the main methods, the public start provide me methods and J PM start the following information in the method. The RM the name of the loader class and its parent class. All that class files related to this class mode, fire methods and variables. And we have three built on class loaders. Aval in Java. The first is the bootstrap class loader. The root class loader is the super super class of extension class loader and loads the standard Java package uh laying ut NE IO and others. These standards packets are present inside the RG dot J and other car libraries in the Java Homi, Jre and Bali directory. The extension class loaded is the subclass of the bootstrap class loader and the superclass of the application class loader dis loads the extensions of the standard Java libraries present in the uh A XC director and the application class loader is the lash class loader.

And the sub class of the extension class loader uh is responsible to load the files present on the class path and the class path is set to the current directory of the application in the loading uh process. The J PM uses the method class loader dot load class for loading the class into the memory and tries to load the class basis on a full. If a parent class loader is unable to find the class delegate the work to a child class loader. And if the child class loader isn't able to load the class, it throws a no class that found error or class, not found exception in the linking process. After a class was loaded into memory, which goes to the linking process. The linking process involves combining the different elements and the pain of the program together. The linking process includes the following steps, verification, preparation and resolution for this class. The verification step, the if these files is properly formed and was generated by a valid compiler or not.

If this verification fails, we get a time action java la verify error and this activity is done by the component byte code verifier. And when this activity is completed, the class is right for compilation in the preparation step, JVM allocations memory for class variable and initially the memory to default values. And uh in the resolution of replacing symbolic Heeren, the process is done by searching through the me the area to allocate the Heeren. In in the initialization step, our stat variables are assigned with their values defined in the code and stat block. This step executed from top to bottom in a class from parent to child in the classier arch. And so we have the bootstrap class loader, the extension and the application class loader that make the process to find the class is loaded by bootstrap bootstrap class loader pass to the extension class loader and for for last password, application class loader. And what requires you to load the class? Uh Now we have we're going to talk about JVM memo that contains five components uh method, the area hat memory JVM language, text program counter hash three and nature methods, text in the method area.

All the class level data as the run time and constant pool field methods, data and code for methods and constructor as storage in this area. If the memory valuable in this area isn't sufficient for the application start-up. The JVM A her there is only one method. The repair of JVM is a shared resource in the heavy area. Here are the all the objects that are related to stains variables and Jarras this memory is shared. Lores in the JVM language text. When a new trend is created in the JV MA separated during time, text is also created at this time, local variables method cows and partial results are storage in the JVM language text a stuck over so occurs when a process being done and a car is a larger stack size, then I wasn't very able available, sorry.

And for 18 method K one and try is made in the stack ma uh it's called stack frame. And when this method C is completed, a stack frame is destroyed. In the stack frame, we have parts, we have local variables, operating stack and frame that in the local variable we have each frame contains an array of variables colored local variables, our local variables and their values are started here at compile time. The length of this that's in the operant text. Let me in the operant text. Each frame contains a less inverse outset college operating text and act as a run time was farm and intermediate operations. In the frame of data, we have our symbols correspond to the meta and start the catch block information. In case of some exceptions, the tr catch block in the program counter uh PC register, each tr has each ounce PC hash which should start the address of the current execution JJ VM instructions. And in the program counter, we have when instructions is it acute? And the program counter register the date with the next instruction the next step of this class of this application in the native method text for each new to separate native methods. In the execution G, we have three parts. We've got three main important parts.

We have the interpreter, the just time compiler and the garbage collector in the execution. John uh is responsible choice. That is the dot class file, uh execution G reads the by code line by line and they use this data and information present in memory added to A Q to these instructions. The interpreter that is the first part of execution. G is responsible to read the the Q to the by code line by line. Do it, do it to this line by line processes is uh some slower process. And when a method call this multiple times every time is necessary a new interpretation, the G compiler that is the just time compiler at this stage, the negative points regarding the layer overcome because the execution giant first use the interpreter as a Q the byte code. And when find some repeated code user, the G just in time compiler ditch compiles out their badge code and convert name. Then just native code is usage for repeated meows that improve the performance. So so first we read line by line uh byte code file and just compile, convert to a native image co. In this compiler, we have four components. We have the inter intermediate code generator, the code optimizer the T code generator and the profile.

Uh The intermit co generator is responsible for generation the inter internet age coach, sorry, the code optimizer responsible for optimize the code for better performance. The target generator is responsible for converge intermediate code to Nature's image coach. Nature coach IO is responsible for finding the hotspots.

Hotspots are code that use as A Q repet many times the same code as A Q. We call this from hotspots and the garbage collector, the garbage collector is responsible for collecting and removing all reference objects from the half memo. This process reclaimed the run time and those of the memory album actually by storing them in the garbage collector, we have two steps. We have the mark and the sweep step in the mark steps, the garbage collector in the file and lo the objects in the memory and the sweep steps, the garbage collector have moved the objects in the files. In the previous step, garbage collector is acute and automa automatically by the JVM at regular intervals. But you can also trigger it by calling system DC. And we have three types of garbage collector. We have the serial garbage collector, the parallel gar garbage collector and the garbage first, the G one garbage collector, serial garbage collector is designed for small applications where a new on single thread environments is the simplest implementation of garbage collector.

The JP N, the argument to use the serial garbage collector is this argument. The parallel garbage is the default type of garbage collector. In J PM uses for multiple threads for garbage collector but still post when the application is running. And the arguments for use the parallel garbage collector is the XX use parallel to C and the garbage first uh recommended for mo to try the applications. Uh When I have a large hip size of A, the ha is divided in size the regions and J one identifies the directs with the most garbage and perform garbage collects on the directs first and J PM argument to use the GJ one Garba garbage collector is XX plus. Use J one GC native methods interface. It's an interface like a bridge that interacts with the native method labors and provide the native labors. And cr C++ for example, it's all OJVM to call CR C++ labors and should be college by CN C++ labors using the native keyword. You can indicate that the method implementation will be provided by a nature of the library and you need to invoke system, load the library to load the shared nature of the library into memory and make these functions are available able to java uh native method libraries and are libraries that are writing in other programming language such as assembly C and C++.

These libraries are user present in the form of dot DOL or dot So files have an example of java compilation execution. Uh For example, in our main class, we have the main F one and F two F one starts in the file as a two dot Java and F two starts the file as a three Java. The com the compiler will compile the three files and produce strict corresponding dot class files. These are these files which consists of byte code. And unlike the C language, no linking is done in the process. On the second image of this compilation execution, the Java VM our virtual machine resides on the RA M memory. And during the ECU uh using the class loader, the class files are brought a memory and the byte code is verified for any security brands. And the last uh step of Java compilation is ECU and the execution DY will convert the byte code into an HEV MA machine code and this is just the time compiling and is one of the main reason is Java is comparatively low than other language. And here we have some hein that are using these presentations and that's all. Thanks for watching. Uh If you have any questions, we have some minutes to the end of time and feel free to connect with me on linkedin other social networks. And thanks for that.