A JVM is a software implementation of a physical machine. That provides an environment to execute compiled Java code (also called byte code). It has an instruction set and manipulates various memory areas at run time.The implementations of JVM vary depending on the operating system.
The Java Virtual Machine knows nothing of the Java programming language, only of a particular bytecodes , the class file format. A class file contains Java Virtual Machine instructions (or bytecodes) and a symbol table, as well as other providing necessary support information.
When Java code is complied into bytecode. This bytecode gets interpreted on different machines
Bytecode is an intermediary language between host system and Java source.
JVM is also responsible for allocating memory space.
As shown in the below architecture diagram, the JVM is divided into three Component:
ClassLoader
JVM Memory
Execution Engine
1) Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. Each Java class must be loaded by a class loader.
When the JVM is started, three class loaders are used:
Bootstrap class loader : loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory.
Extensions class loader : loads the code in the extensions directories <JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property).
Application or System class loader : loads code found on java.class.path, which maps to the CLASSPATH environment variable.
Linking : performs verification, preparation, and (optionally) resolution
Verify – Bytecode verifier will verify whether the generated bytecode is proper or if not, occur verification error.
Prepare – allocates memory for class variables and assigned with default values.
Resolve – symbolic memory references are replaced with the direct references.
Initialization : Java code initializes class variables (static variable or static blocks) to their proper initialization values.
2) JVM Memory is divided into five major components:
Method Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods. There is only one method area per JVM, and it is a shared resource.
Heap Area is the runtime data area. All the Objects and their corresponding instance variables stored. There is also one Heap Area per JVM and shared by multiple thread.
Stack Area stores frames. It holds local variables and partial results, and plays a part in method invocation and return.Each thread has a private JVM stack, created at the same time as thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed. Each thread will have separate PC Registers.
Native Method Stack hold all the native methods information which is used in the application.
3) Execution Engine reads the bytecode and executes it piece by piece :
Interpreter interprets bytecode stream then execute the instructions.
Just-In-Time(JIT) compiler:It is used to improve the performance. The Execution Engine will be using the help of the interpreter in converting byte code, but when it finds repeated code it uses the JIT compiler. So reduces the amount of time needed for compilation.
Garbage collection is the process by which Java programs perform automatic memory management. Garbage collector collects and removes un-referenced objects. Garbage collection of the JVM collects the objects that are created .
Native Method Interface is an interface that connects to Native Method Libraries and provides the Native Libraries required for the Execution Engine.
Native Method Libraries: This is a collection of the Native Libraries, which is required for the Execution Engine.
JRE provides environment for running/executing programs. It is the implementation of JVM. It physically exists. It contains set of libraries and other supporting libraries that JVM uses at runtime.
JRE = JVM + class libraries (rt.jar) + other libraries (if any).
JRE acts as a software layer on top of an operating system that enforces uniformity and provides additional Java services, such as automatic memory.
It also includes:
Deployment technologies like Java Web Start and Java Plug-in.
User interface toolkits like Java 2D.
Integration libraries like Java Database Connectivity (JDBC) and Java Naming and Directory Interface (JNDI).
Lang and util libraries like Java Archive (JAR).
Other base libraries like Java Management Extensions (JMX), Java Native Interface (JNI) and Java for XML Processing (JAX-WS).
The JDK includes a private JVM and a few other resources to finish the development of a Java Application .It physically exists. It contains JRE + development tools.
The JDK has as its primary components a collection of programming tools, some of components below describe:
appletviewer – this tool can be used to run and debug Java applets without a web browser
apt – the annotation-processing tool
java – the loader for Java applications. This tool is an interpreter and can interpret the class files generated by the javac compiler. Now a single launcher is used for both development and deployment. The old deployment launcher, jre, no longer comes with Sun JDK, and instead it has been replaced by this new java loader.
javac – the Java compiler, which converts source code into Java bytecode
javadoc – the documentation generator, which automatically generates documentation from source code comments
jar – the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files.
keytool – tool for manipulating the keystore
xjc – Part of the Java API for XML Binding (JAXB) API. It accepts an XML schema and generates Java classes.