Before directly move to Java Virtual Machine, we need to understand what runtime environment is and also understand some basic stuff i.e.

What is runtime?

  • In a simplest term it is a system used by operating system which takes care of converting the code that you write in a high level language like Java to machine code and understand by CPU/Processor.
  • Runtime comprises of software instructions that execute.
  • CPUs or more general term our computers understand only machine language (binary codes) so to make it run on CPU, the code must be converted to machine code.

JVM

  • Java Virtual Machine is a runtime engine. It’s a virtual machine that enables to run java programs and provide runtime environment in which java byte code can be executed.
  • Java Virtual Machine is an implementation of JVM specifications, interprets complied java binary code i.e. byte code for a hardware so that java program can be performed.
  • JVM is heavily used, and maintained by bright engineers, both open source and corporate.
  • Java is called platform independent because of Java Virtual Machine.
  • Different operating systems have their JVM Java interprets byte code into machine level language whenever we submit a .class file to operating system.
  • JVM is OS i.e. operating system dependent. It is JVM’s responsibility to make same class files run on any other operating system.
  • JVM is responsible for allocating and deallocating memory space used by java programs.
  • Main function of JVM is to load, verify and execute file.

JVM consists of three notions:

  • Specifications: Document which consists of implementation of JVM.
  • Implementation: Java Runtime environment (JRE).
  • Instance: An instance of java is created whenever we run java class file.
  • Run Time Data Areas:

Heap and Stack memory spaces

JVM designates memory to these operations from either Stack Memory or Heap Space, whenever we declare new variables and objects, call method, declare a String or perform similar operations.

Heap:

  • Heap is a place where the objects and its instance variable are stored.
  • Heap is shared among all the threads i.e. all parts of applications and contain objects, classes’ metadata etc.
  • The amount of heap JVM demands from Operating System can be controlled.
  • Heap memory is long lived.
  • -Xmx and -Xms -> These are the two option to define maximum size and startup size of heap respectively.
  • Objects stored in heap are globally accessible.

Example:

  public class Heap 
{
     public static void main(String[] args)
         {
              Heap ref= new Heap();
               ref.f1();
         }
     void f1()
      {
           String str1=”Heap Memory”;
           System.out.println(Rahul);
       }
 }          
Heap

Stack:

  • Stack is a memory place where the methods and the local variables are stored.
  • Stack memory is used by only one thread. A StackOverflow error can occur if a thread demands more stack space than is permitted. If the stack is dynamically expandable, it can still throw OutOfMemoryError.
  • Stack memory is short lived.
  • -Xss is used to define stack memory size
  • Stack memory can’t be accessed by other threads.

Example:

 public class Stack()
{
      public static void main(String[] args)
       {
           int x=4;
           int y=6;
           Stack ref = new Stack();
           ref.f1();
       }
    void f1(int Mandeep)
       {
            int z=10;
            System.out.println(Mandeep);
         }
}
Stack

That’s all I have and thanks a lot for reading. Please let me know if any corrections/suggestions. Please do share and comments if you like the post. Thanks in advance… 😉


Thanks Sonali for helping us to grow day by day. She is passionate to solve challenging problems and loves to play around Java.

Categories: Miscellaneous

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert