Java Runtime Environment (JRE): Java is everywhere in browser, in mobile, in TV or in set-top boxes and if you are into Java programming language than you know that Java code which is bundled in JAR (Java archive) file require Java virtual machine JVM to execute it. Now JVM is an executable or program like any other program and you can install that into your machine. You have seen browser often suggesting download JRE to run a Java Applet downloaded from Internet. Various version of JRE are available in java.oracle.com and most of the user who just want to execute Java program inside browser or standalone downloads JRE. All browsers including Internet Explorer, Firefox and Chrome can work with JRE.
Java Virtual Machine (JVM): When you download JRE and install on your machine you got all the code required to create JVM. Java Virtual Machine is get created when you run a java program using java command e.g. java HelloWorld. JVM is responsible for converting byte code into machine specific code and that's why you have different JVM for Windows, Linux or Solaris but one JAR can run on all this operating system. Java Virtual machine is at heart of Java programming language and provide several feature to Java programmer including Memory Management and Garbage Collection, Security and other system level services. Java Virtual Machine can be customized e.g we can specify starting memory or maximum memory of heap size located inside JVM at the time of JVM creation. If we supplied invalid argument to java command it may refuse to create Java Virtual Machine by saying "failed to create Java virtual machine: invalid argument". In short Java Virtual Machine or JVM is the one who provides Platform independence to Java.
Java Development Kit (JDK): JDK is also loosely referred as JRE but its lot more than JRE and it provides all the tools and executable require to compile debug and execute Java Program. Just like JRE, JDK is also platform specific and you need to use separate installer for installing JDK on Linux and Windows. Current Version of JDK is 1.7 which is also referred as Java7 and it contains javac (java compiler) based on programming rules of Java7 and Java which can execute java7 code with new features like String in Switch, fork-join framework or Automatic Resource Management. When you install JDK, installation folder is often referred as JAVA_HOME. All binaries are located inside JAVA_HOME/bin which includes javac, java and other binaries and they must be in your system PATH in order to compile and execute Java programs. For details on Path see how to set PATH for Java in Windows and UNIX.
Difference between JRE, JDK and JVM
In short here are few differences between JRE, JDK and JVM: 1) JRE and JDK come as installer while JVM are bundled with them. 2) JRE only contain environment to execute java program but doesn’t contain other tool for compiling java program. 3) JVM comes along with both JDK and JRE and created when you execute Java program by giving “java” command.
Just in Time Compiler (JIT): Initially Java has been accused of poor performance because it’s both compiles and interpret instruction. Since compilation or Java file to class file is independent of execution of Java program do not confuse. Here compilation word is used for byte code to machine instruction translation. JIT are advanced part of Java Virtual machine which optimize byte code to machine instruction conversion part by compiling similar byte codes at same time and thus reducing overall execution time. JIT is part of Java Virtual Machine and also performs several other optimizations such as in-lining function.
JRE (Java Runtime Environment) provides you all necessary things that are required to execute a java program. It consists of three main components.
- · JVM
- · Core java classes
- · Java Platform Libraries
JVM: JVM is an acronym of Java Virtual Machine. JVM is a virtual environment that executes your java program. Very popular phrase “Write Once, Run Anywhere (WORA)” is true only because of JVM. JVM is set of instructions and provides guidelines to vendors to provide implementation. There is a very long list of JVM implementations but rarely you will get a chance to work with except Hotspot JVM. Hotspot JVM is provided by Sun Micro System which was taken over by Oracle.
So here we will focus on Hotspot JVM only. You may have heard Java is platform independent. Right? So, java achieve this platform independency. JVM is the answer.
Figure-1: High level, and theoretical steps of execution of a java program.
If you have a closer look at the image above, you can easily figure out that JVM is not platform independent, but it makes your java program platform specific.
Figure-2, displayed major components into a JVM.
1. Class loader Subsystem: which is responsible for loading a class into memory. We will cover this concept later. If you are curious about it right now, click Here.
2. Memory Area: Where your object data is stored, it consists of multiple memory sections. We will discuss memory architecture very soon in this article only.
3. Execution engine: Responsible for executing your program and resource management. Main components are of Garbage Collector, JIT etc.
Memory Model in Java: JVM memory is divided into different segments. Some of them are here.
- Method Area – All the class level data will be stored here, including static variables. There is only one method area per JVM, and it is a shared resource.
- Heap Area – The heap is where your object data is stored. This area is then managed by the garbage collector selected at startup. Most tuning options relate to sizing the heap and choosing the most appropriate garbage collector for your situation. All the Objects and their corresponding instance variables and arrays will be stored here. There is also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread safe.
- Stack Area – For every thread, a separate runtime stack will be created. For every method call, one entry will be made in the stack memory which is called as Stack Frame. All local variables will be created in the stack memory. The stack area is thread safe since it is not a shared resource.
- PC Registers – Each thread will have separate PC Registers, to hold the address of current executing instruction once the instruction is executed the PC register will be updated with the next instruction.
- Native Method stacks – Native Method Stack holds native method information. For every thread, a separate native method stack will be created.
Execution Engine:
1. Interpreter – The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the interpreter is that when one method is called multiple times, every time a new interpretation is required.
2. JIT Compiler– The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine will be using the help of the interpreter in converting byte code, but when it finds repeated code it uses the JIT compiler, which compiles the entire bytecode and changes it to native code. This native code will be used directly for repeated method calls, which improve the performance of the system.
3. Garbage Collector: Collects and removes unreferenced objects. Garbage Collection can be requested by calling "System.gc()", but the execution is not guaranteed. Garbage collection of the JVM collects the objects that are created.
4. Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native Libraries required for the Execution Engine.
No comments:
Post a Comment