fbpx
Select Page

Internals of JVM – How it works?

Hello friends, my name is Pruthwiraj Ghadge and I am working as Java Trainer in SevenMentor Pvt. Ltd. for the last 2 months. Today we are going to talk about the JVM Architecture. This is one of the hottest topics in the Java fraternity that always goes around in discussion.

As a Java programmer, we always wonder about how it works and how it runs on any platform without any intricacy. I always wanted to know about the working of the JVM in detail so I worked upon it and now I know it, a little bit. It’s true that becoming a Java Programmer is not a child’s play. You have to work very hard and learn the basics and clear the foundation. When you are done with that part, you are ready to learn the advance thing. But when we talk about the advance part it is very necessary to know the working of JVM.

JVM plays the most important role in Java Development. When writing code in other languages like C, C++ we need specific compilers for the languages and that to specific to the Operating Systems. While writing programs in C and C++ we may face problems of incompatibility and platform dependency. However, Java has overcome that problem with the help of JVM and hence it proves to be one of the most powerful tools in the programming world. Program written in C and C++ may need to be changed on different platforms to suit their needs and also they need to be recompiled again for generating platform-specific intermediate code. Whereas Java has completely changed the scenario and provides a solution to this with JVM where you can write programs on any platform and run them anywhere on any platform-Operating System.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

This means that JVM and class files together provide Java a platform-independent nature.

So, what is JVM basically?

JVM stands for Java Virtual Machine. As we know that to run any application we need a platform. For example, to run any Operating System we need a machine i.e. hardware platform. To run any software we need an Operating System that means a software platform. Similarly, we need a runtime platform for executing the Java programs and that platform is nothing but JVM-Java Virtual Machine. But why Virtual Machine? When we say machine it refers to a physical set of hardware, however, JVM is not hardware but it is a software that resembles a simulation of a physical hardware platform. And hence it is a Virtual Machine that runs Java Programs on it. 

Who writes the JVMs?

JVM is an Operating System specific implementation and hence the OS vendors generally write the JVMs. There are JVMs present in the market which are developed for Microsoft Operating Systems, Unix Flavoured Operating Systems, Solaris and Macintosh Operating Systems. These are some of the most commonly used operating systems in the world and hence their specific JVMs have been written by those OS providers. As every different OS has its own different directory and file structure they need to have their own JVM specifications. And hence with the core elements provided by Oracle, these companies or vendors write their own specifications.

What makes JVM unique?

Although JVMs are operating system specific implementations, they provide the biggest advantage to Java making it portable over almost all operating systems. Java programs are called as WORA – Write Once Run Anywhere – programs. JVM is the only reason why it is possible. All the JVMs share a unique set of instructions, using which the class file is created after the compilation of the program. These class files are portable in the actual sense. Class files can not be created using any other instruction outside of JVM and they can not be modified from outside by any means and hence the class files are very secure. We can say that Java is one of the most secure programming languages. 

When the Java Program is compiled a ‘.class’ the file is created which contains the Platform Independent code which is known as Byte Code. This code is the same across all the platforms and in the real sense, this code makes Java a Platform Independent and Architecture Neutral language. JVM has one specific set of instructions that creates this byte code and no other ways we can create the byte code and that is the reason why Java is termed as the most secure language in the Programming world.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Architecture of JVM

JVM Archtecture

The above diagram shows the basic architecture of the JVM. JVM is divided into 3 basic parts, ClassLoader Subsystem, Runtime Data Areas and Execution Engine with Native Method Interface and Libraries.

Let’s discuss each unit in brief.

  • ClassLoader Subsystem

ClassLoader Subsystem is the first part of JVM. This part loads the class file generated by the compiler and does various tasks related to verification, memory allocation and loading of class into the memory.

  • Runtime Data Areas

Runtime Data Areas are the areas in the memory where different parts of the programs are allocated the memory. They are divided into five types, Method Area, Heap, Java Stacks, PC Register, and Native Method Stacks.

  • Execution Engine with Native Method Interface and Native Method Libraries

This part of JVM is responsible for the execution of the Java program i.e. class file. This part coordinates with the Native Method Interface to run any Native Method code.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Let’s have a brief look at these parts now.

ClassLoader Subsystem

ClassLoader Subsystem is a part that is mainly taking care of loading the class files and allocating the memory to the different parts of the program. First of all, it loads the class file generated by the javac compiler into the memory and then checks for the validity of the file. As we earlier mentioned that JVM has a specific set of instructions using which it creates the class files, it checks the integrity of the code with those instructions and if found any malfunctioning it does not allocate the memory to the respective parts of the program. If everything is fine and there are no discrepancies it allocates the memory. Memory allocation takes place in the given Runtime Data Areas which we will discuss now.

Runtime Data Areas

When the class is loaded by the ClassLoader Subsystem memory allocation takes place in the respective data areas. Let’s have a look at these data areas and see what is stored wherein the memory.

Method Area

Method Area is that place in the memory where the names of the classes, variables, modifiers and method information are stored. 

Heap Area

Heap is the memory portion in the memory that stores the runtime data object. It stores the data for the objects and any other thing that is created at the time of execution. While the program is running we create objects and these objects get memory allocated in the heap memory.

Java Stack

When the program begins its execution a memory part is needed to store the execution-related data. For each new method(function) a new stack frame is created and the data for those methods is stored in those stack frames. When the program begins its execution the main method starts and the main stack is created for that method. When the methods finish their execution their stack frames are deleted.

PC Register

As the program runs, CPU executes the instructions one at a time. While CPU is executing one instruction the JVM keeps the next instruction ready for execution. However, CPU does not know where to fetch that instruction from and hence the JVM keeps the track of next instruction in the PC register. While CPU is executing the current instruction PC Register holds the address of the next instruction to be executed. As the current instruction completes its execution next instruction is fetched by the CPU from the address stored in the PC Register. And then JVM stores the address of the next instruction in PC Register and the execution continues. 

Native Method Stack

While writing programs in Java we may also come across writing some native language code. Native language means C and C++. As Java has been developed from C and C++, these languages are called and native languages and Java supports coding in these languages. However, JVM is unable to compile this code hence these codes are given separate memory and that is called Native Method Stack. All native methods are run in these stacks.

These the different memory areas and working areas where memory to different elements of the program is allocated and different tasks are done.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Execution Engine with Native Method Interface and Native Method Libraries

Execution Engine is the part of the JVM which is responsible for the execution of the java program. Execution Engine makes the coordination with the Native Method Interface and executes the program. Native Method Interface loads the Native Method Libraries and executes the Native Method codes.

Apart from the above-mentioned things, there are many other internal details still not explored here which we will talk about in some other blog. For now, that’s it from my side, thanks for reading.

Happy Coding…!!!

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Call the Trainer and Book your free demo class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

Pin It on Pinterest

× How can I help you?