Skip to main content

Internal Processes of Java Program


We already know that a java file i.e. the source code is to be converted into class file i.e. byte code. Byte code is a highly optimized set of instructions designed to be runned/executed by something called Java Virtual Machine, the JVM. Earlier or you can say, originally JVM was only the interpreter for byte code.

Why is Java needed?

         Java solved the major problem of security and portability by converting the source code into bytecode. this bytecode can be executed anywhere provided JVM is implemented. The execution of bytecode or class file  by the JVM is the most simple way to create portable programs.
                 Bytecode also makes Java, secure. Since there is only one condition for a java file(here bytecode) to execute successfully that is JVM should be implemented into the machine where the bytecode is going to be executed.  Without JVM, bytecode is not going to be executed which, in turn becomes safe.

Internals of JVM

          JVM being a part of JRE(Java Runtime environment)acts as a runtime engine to run Java apps.  It actually calls the main method from the Java file (main method is the entry point of Java class). Earlier we had discussed about WORE that is write once run anywhere. All this is possible because of JVM which helps setup JRE.Click Here...

What tasks are performed by JVM?

Architechture of JVM

Basically all the tasks of JVM are divided into three subcategories:-
  1. Class Loader system
  2. Runtime Data Area
  3. Execution Engine.
1. Class Loader System:
                   A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
Every Class object contains a reference to the ClassLoader that defined it.
Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.
Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
Class loaders may typically be used by security managers to indicate security domains.
Class loaders that support concurrent loading of classes are known as parallel capable class loaders and are required to register themselves at their class initialization time by invoking the ClassLoader.registerAsParallelCapable method. Note that the ClassLoader class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable.
In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see loadClass methods).


2. Runtime Data Areas:
              During execution of a Java Program, JVM define various runtime data areas. Some data areas are created on JVM startup and some are created as per thread startup send all these data areas when JVM exits.
      The different runtime data areas are PC register, JVM stacks,  heap, method areas, runtime constant pool and native method stacks.
a. PC Register:- JVM supports multithreading that is it can execute many Threads at once.The address of currently executing JVM instruction stored in PC register only if the thread is not native. If it is native the value of PC register is undefined.

b.  JVM stacks:-
         Before said, JVM defines runtime data areas on startup and destroys those data areas when it exits. This means that each JVM thread creates a private JVM stack. These stacks frames (methods) that is it holds variables, partial results, invokes methods, returns results.
 Exceptional conditions with JVM stacks
  1.  If computation in a thread requires a larger JVM stack than is permitted, the JVM throws a StackOverflowError.
  2.  In case of insufficient memory, the JVM throws OutOfStackError

c. Heap:-
         Heap is the runtime data from which memory for all class instances and arrays is allocated. Heap memory too is created on JVM startup. This memory among all Java Virtual Machine threads. The heap memory  expand or contracts as per computational requirements.
 Exceptional conditions  associated with heap memory:-
  1.  If the computational requirements of the heap memory goes beyond the maximum heap size, the Java Virtual Machine throws and OutOfMemoryError.
d. Method area : method area is similar to Storage Area of compiled code of a language. It stores structures such as the runtime  constant pool, field and  method data and the code for method and constructors as well as special methods in class initialisation and interfere interface initialisation.

 Method area exceptional condition: if method area not make memory available for an allocation request, the JVM throws an OutOfMemoryError

e. Native method stacks: first of all what are native methods? Method written in a language other than Java  are called native methods. Generally the native method  stacks are used by the implementation of an interpreter  for JVM's  instruction set  in a language such as C. For example, if you want to  incorporate some HTML code into a java method, this Java method becomes a native method since it has a code  other than Java. Then the JVM  utilizes native method stacks of the memory.  The programmer may  initially  not know  size of native method,  the stack may  expand or contract as per requirement.
 Exceptional conditions of  native  method  stacks:

  1. If any particular thread  computation requires more space than permitted, the JVM throws stack overflow error
  2. If in some case native method stack is expected  to expand Birds memory is insufficient or if insufficient memory made available 8 initial stacks for a new thread, JVM throws out of memory error.


So, this is all about internal details of JVM in brief!.
Continue in the next post!!See you till then...byeeeeeee!! 

Comments