Core Java - JVM Tutorial

JVM - Java virtual machine

JDK - Java Development Kit
It is a software which contains all the programs required for compilation, execution, debugging etc. JDK contains tools to develop the Java program and JRE to run the program.
JRE - JRE stands for Java Run-time Environment
JRE is targeted for execution of the Java files that is JRE is nothing but JVM with the Java Packages Classes(like util, math, lang, awt,swing etc) with addition of runtime libraries. JDK is mainly targeted for java development purpose.
JVM - Java Virtual Machine
It executes the whole program.
It provides a platform to run the program.
JIT - Just In Time
Normally the source code is completely converted into the machine code. In JIT Scenerio, the source code is converted to assembly language like structures (intermediate language for C# or Byte code for Java or P code for Pascal). The intermediate code is only converted to machine code only when the application needs i.e. fraction of codes which are required are only converted to the machine code. With JIT, CPU burden is optimized, instead of converting all the source code to machine code, it converts necessary part of the code. If the function or method called is not in machine then that part is converted by JIT.
In JAVA, JIT is in JVM. In Android JIT is in (DVM) Dalvik Virtual Machine or ART (Android Run Time) in newer versions.
JIT improves the performance of Java applications at run time.
Library

It contains ready-made programs.
Class loading
  • Loading a class from hard disk to JVM is known as class loading.
  • Class loader loads the class where a class is used for the first time execution.
  • When the class is loaded memory is allocated to all the static variables of that class.
  • The class loader is a sub-program of JRE.
  • Class loading is dynamic, whenever the new class is used in the execution class loader will load that particular class from hard disk to JVM.
  • Maximum one public class is allowed in JAVA file.
  • A static pool of class area is utilized for static variables.
Java stack and heap memory management
Consider the following program
public class MemoryClass {

    public static void main(final String[] args) {
        int i = 0;
        MemoryClass memoryClass = new MemoryClass();
        memoryClass.myMethod(memoryClass);
    }

    private void myMethod(final Object obj) {
        int i = 1;
        String s = "HelloWorld!";

    }

}
Where are the methods of s stored?
They are stored in the String class object; it is an object loaded by a ClassLoader object when String is first referenced in the program. All the implementations of the JVM that existed when I read about this last never deallocated the memory for a class object once it was loaded. It's on the heap.

Would JVM free the memory allocated to myMethod as soon as it's execution is completed, if so, how would it manage the situation mentioned in question 2(only applicable if JVM allocates memory multiple times to the same method).

If you declare a variable and never use it, it is quite possible (and common) for the compiler to notice that there is no use for it and to not put it into the class file. If it isn't in the class file, it is never referenced, and therefore it and its methods are not loaded, etc. If the compiler puts it in anyway but it is never referenced, then the ClassLoader wouldn't have any reason to load it, but I'm a little vague on whether it would get loaded or not. Might depend on the JVM implementation; does it load things because there are variables of the class or only when they are referenced? How many ClassLoader algorithms can dance on the head of a 4-digit PIN?

Nonstatic variables and methods
  • If we create an object multiple times, the nonstatic variables are created the same number of times.
  • Another name of the object is an instance.
  • For every instance, there is a copy of nonstatic variables. So nonstatic variables can't be executed using the class name.
  • Nonstatic variables are allocated in the heap area when objects are created.
class A{
static int i;
int j;
static void m1(){
System.out.println(i);
System.out.println(j); // because inside static method we can't refer nonstatic variable of same class.
}
}

outcome
compile time error
void m2(){
System.out.println(i); // static variable can be accessed inside nonstatic member
System.out.println(j); // non static method can directly refer nonstatic member of same class.
}

class D{
int i;
static void display(){
System.out.print(i); // a non static field can't be referred inside a static memeber. because there is no guarantee that object of this class will be created.
}
}


class B{
in i;
void m1(){
system.out.print(i);
}
}
class test1{
public static void main(String arg[]){
B b1 = new B();
b1.i = 10;
B b2 = new B();
b2.i = 20;
b1.m1();
b2.m1();
}
}

output
10
20
Below a simple illustration of the memory allocation of instances and data members of the above program is given,

class C{
static int i;
int j;
void increment(){
++i;
++j;
}
void display(){
System.out.println(i);
System.out.println(j);
}
}
ClassTest2{
public static void main(String[] arg){
C c1 = new C();
C c2 = new C();
C c3 = new C();

c1.increment();
c2.increment();
c3.increment();

c1.display();
c2.display();
c3.display();

}
}

output
3
1
3
1
3
1

Comments

Popular posts from this blog

Java Variables and Datatype

JAVA Features

Java Fundamentals