What is Java Virtual Machine?
It is necessary for the programmer to understand the Java Virtual Machine (JVM) and how it works. This will help them to take advantage of JVM and avoid its weakness.
The virtual machine is a software concept based on the idea of an imaginary computer. It has a logical set of instructions. These instructions define the operation of the imaginary computer. It can be through as a mini operating system. It forms a layer of abstraction for the underlying hardware platform. The operating system, and the compiled code.
The computer converts the source code into a code based on the imaginary computer’s instruction set that is not targeted to a particular processor. An interpreter is an application that understands these streams of instruction. It converts these instruction for the underlying hardware to which the interpreter is targeted. The JVM internally creates a runtime system that help the execution of code by:
Loading the .Class files
Class loaders are one of the fundamentals of the Java Virtual Machine (JVM) architecture. They enable the JVM to load classes without knowing anything about the file system semantics, and they allow applications to dynamically load java classes as extension modules.
Managing the memory
The java virtual machine (JVM) manages memory in the following way:
- When a JVM is invoked to run an application, it requests the operating system for enough memory for the JVM itself for running and free memory for the application to create new object
- When a new object is created, the JVM allocates memory for that object from the free memory area.
- When the free memory area is reduced after several object creations, the JVM ask the operating system for more.
- When an object is no longer used, it will destroyed. The memory occupied by it will be free up and it will be merged back to the free memory area.
- When the free memory area is occupied, and no more additional memory is available from the operating system, the JVM will halt the application that created the situation, and will raise ”Out of memory error”.
Performing the garbage collection
The garbage collector release the memory occupied by an object once it determine that the object is no longer accessible. This automatic process make it safe to throw away unnecessary object references because the garbage collector does not collect the object if it is still needed elsewhere. Therefore, in Java the act of releasing unnecessary references never runs the risk of deal locating memory prematurely.
When the JVM is executing code, one local register called “Program counter” is used. This register points to the currently executing instruction. If needed, the instruction modify the program counter to alter the flow of execution. Otherwise, the slow is sequential, that is, it flows from one instruction to the other.
The other popular concept in Java language is the use of the ‘Just-in-Time’ (JIT) compiler. Browsers like Netscape Navigator and Internet Explorer include JIT compilers that increase the speed of Java code execution. JIT’s main purpose is to convert the byte code instruction set to machine code instructor targeted for a particular microprocessor. These instructions are stored and used whenever a call is made to that particular method.
The above figure shows the relationship between java compiler and JIT compiler.