Monday, April 28, 2014

JVM

Some Points you would love to know about JVM internals:
1) when we download JDK from oracle site and install it on machine then we get the JVM provided by the Oracle vendor same way IBM also provide its JVM and when you use linux then also there is Open JDK.  That means there must be some abstraction or abstract specification which is implemented by these vendors. so when we talk about JVM three points come in light.

  1. Abstract Specification that is implemented by different vendors
  2. The concrete implementation provided by the different vendors like oracle, IBM and many more.
  3. The runtime instance : the host where the java application will be running.
2) When java application starts the runtime instance is created and it dies with the completion of the program.Each java application runs inside its own Java virtual machine. i mean when if you will start three java application at same time on same computer then you will get three java virtual machine instances.
 
3) A JVM instance starts with the invocation of the main method of an application and with the invocation of main method a initial thread starts and which can run the other threads also.
 
4) In JAVA there is two types of thread one is daemon thread and other is non-daemon thread. Daemon thread is thread that is used by the JVM itself such thread is thread that perform garbage collection. Any thread can be marked as daemon thread. The initial thread that starts with the execution of main method is termed as the non-daemon thread.
 
5)When we talk about JVM instance then it is described in terms of subsystem,memory areas,data types and instruction set and these components describe the internal architecture for abstract java virtual machine.
 
6) Each JVM has class loader subsystem that provides the loading mechanism for the types  that is class,interface,enums and annotations. Apart from that there is execution engine which helps to execute the instruction contained in methods of loaded classes.
7) After loading the class when we run a program , JVM needs memory to store many things including byte-codes , object created, method parameters return values ,local variables and intermediate result of computations. and these required memories are organized into several runtime data areas.

8) Each instance of JVM has one method area and one heap which are shared by all threads running inside the java virtual machine.
 
9) When the virtual machine loads a class file, it parses information about a type from the binary data contained in the class file. It places this type information into the method area. As the program runs, the virtual machine places all objects the program instantiates onto the heap.

10) As each new thread comes into existence, it gets its own pc register (program counter) and Java stack. If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute. 

11) A thread's Java stack stores the state of Java (not native) method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. 

12)The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method. 

13)The Java virtual machine has no registers to hold intermediate data values. The instruction set uses the Java stack for storage of intermediate data values. The stack-based architecture of the Java virtual machine's instruction set facilitates the code optimization work done by just-in-time and dynamic compilers that operate at run-time in some virtual machine implementations. 
thread 1 and 2 executing java methods and thread 3 executing the native methods.
 

No comments:

Post a Comment