JVM is the platform where Java programs run and it is the one which makes java program as platform independent.It is used in many,many different ways,from desktop applications to online real time trading systems.
- JAVAC compiler compiles the java code and give java byte as output which is given to JVM.
- Load the java byte code .class files into the JVM via the Class Loader.
- JVM finds the main method and accumulates local variables, object variables, and reference variables on the stack.
- Execution engine executes the bytecode with the support of native method libraries and native method interface.
Some of the main features of Java virtual machine are,
- It sits between operating system and your programs/applications.
- It is the JVMs responsibility that makes it possible for the same class file to run on any other operating Systems.
- Other languages such as C++,Objective C need to be compiled to run with a defined host operating system and architecture.But it is not the case with java.On whichever platform java code complies,it can run on any versions of JVM on any OS(MAC,Windoes,Linux).
- Garbage collection
- is a class instance which is explicitly created by the java code. It can be triggered by calling “System.gc()”
- is a mechanism of reclaiming the previously allocated memory,so that it can be reused by future memory allocations.
How JVM works
1.Class loader : Class loader takes .class file and convert it into bytecode which can be saved in memory area.And main activity of class loader is,
- load/reading bytecode
- verifying bytecode
- linking the bytecode to library
2.JVM memory consists of 5 areas.These are the runtime data area of JVM
- Method area
- It is shared memory among all the classes which holds all class level information such as static variables.
- Heap area
- All class objects and their related variables and arrays are stored here.And there is only one Heap area per JVM.
- Data stored in heap area is not safe because heap and memory area shares the memory for multiple threads.
- The keyword “new” allocates memory in heap.It is the main pool of memory and accessible to the whole of application.
- If there is no enough memory ,then JVM tries to recclaim the memory from the heap with garbage collection.
- Stack area
- A separate runtime stack will be created for every threads.
- All local/temporary variables will be stored in the stack area.
- PC registers
- It will hold the address of current executing instruction.
- Each thread will have specific PC register.
- Native method stacks
- For every thread,seperate native method stack will be created.
- It holds native method information and It can access runtime data areas of JVM.
3.Execution engine : It takes bytecode from memory area and execute it.
4.Native method interface : It enables java code to call or be called by native applications which are specific to the hardware and OS of a system.
5.Native method libraries : A collection of the Native Libraries which is required for the Execution Engine.