Java and JVM Internals

One of the main issues C and C++ like languages are facing is output of program is executable code. This executable code is fully depends on platform on which program is getting compiled. And we have to have different executables for different platforms. Hence these languages are not great for portability, so these are not much useful in internet world, where different platforms are connected to same web world. Then how to solve this problem? The solution is provided by Java – bytecode.

Bytecode

As we have already discussed in our previous lesson Java compiler’s output is .class file not executable. What is present in this .class file? Java compiler generates a intermediate language or instructions as per our program and write them to .class files. These instructions are called bytecode.

This bytecode is independent of platform on which you are compiling your program. Now you might get a question that how is my class file is executing if it not executable code?. The answer is Java runtime system called as Java Virtual Machine.

Java Virtual Machine (JVM)

Basically JVM is designed to interpret bytecode. This JVM understands bytecode and executes it depends on platform on which you are executing Java program. Hence you write and compile once on one platform and executes multiple times on multiple platforms. Now if you want to send your program to your friend who is having different platform than yours, you just need to send .class files and your friend should have JVM installed in his/her machine. How cool it is isn’t it?

As JVM fully controls execution of Java program assists in making it secure. JVM prevents from generating side effects and it is also improved by several restrictions exists in Java language.

Yes, JVM interprets bytecode. You might have already learned that interpreter is much slower than compiler, that’s why majority of languages use compilers to execute fast. But bytecode is highly optimized code so interpret executes them faster and speed is almost equivalent to a compiler. On top of that Java uses just-in-time (JIT) compiler to execute parts of byte code.

In Java it is practically not possible to compile entire Java program to executable code. Because Java has to perform various runtime validations. So not all code is fed to JIT for compilations, only part of the code which can be benefited from JIT is fed to it. Remaining part is interpreted. Even though JVM interpreter is fast, JIT also gives some boost in performance to make JVM faster.

What is in JVM?

Now we understood what is JVM and why it is useful. Now let’s get overview of what is in JVM. For every execution of a program an instance of JVM is created. This instance contains different segments. They are:

  • Class Loader
  • Class Area
  • Heap
  • Stack
  • Program Counter Register
  • Native Method Stack
  • Execution Engine

Class Loader:

This subsystem of JVM is responsible for loading required class files into memory.

Class Area:

This area is responsible or contains complete information about all classes such fields, method data, code for methods and runtime pool etc.

Heap:

This area used to allocate memory for objects in run time.

Stack:

This memory area holds different information such as local variables of a method, holds information related to method invocation and return values of methods. This memory is cleared once method execution completes.

Program Counter Register:

It holds address of JVM instruction currently being executing.

Native Method Stack:

It contains all native methods useful for application.

Execution Engine:

This is main part of JVM which executes code. It contains a virtual processor, interpreter and JIT.

<<< Your first Java ProgramJava Keywords and Best Practices >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .