What is Java virtual machine?

Java Virtual Machine (JVM) is a specification that provides runtime environment in which java  bytecode can be executed. As the name implies, the JVM acts as a “virtual” machine or processor. Java‘s platform independence consists mostly of its Java Virtual Machine (JVM) . JVM makes this possible because it is aware of the specific instruction lengths and other particularities of the platform. The JVM performs following operation:

  1. Loads code
  2. Verifies code
  3. Executes code

In most cases, other programming languages, the compiler produce code for a particular Operating System but the Java compiler produce Bytecode only for a  Java Virtual Machine . When you run a Java program, it runs as a thread within the JVM process. It is the JVM‘s responsibility to load your class files, verify code, interpret them and execute them. When you issue a command like java , the JVM loads the class definition for that particular class and calls the main method of that class.

It is the JVMs responsibility that makes it possible for the same class file to run on any other  Operating Systems . The JVM takes your compiled platform-neutral byte code and interprets it to run platform-specific machine code. It can also compile it into native code with a  JIT (a  just-in-time compiler that compiles and caches your code, usually one method at a time). Thus, it is in the JVM where your code results, if needed, in native Operating System calls. Therefore, in the  JVM , your platform-neutral threading code gets turned into platform-specific threading code.

Java allocates  threads as needed for your application. The JVM manages the memory of your Java program. So, when you create an Object, or an Audio Clip, or a plain old float, Java allocates memory for both objects and primitives. Java determines when these items are no longer referenced, and, therefore, can have their memories reclaimed. The JVM, without any prompting from the user, runs the  Garbage Collector thread (when possible or required) to reclaim used, unreferenced memory. In addition to interpreting bytecodes, the JVM must supply interfaces to the various subsystems managed by the  Operating System for display, mouse, keyboard, file system and I/O ports etc.

JVM Architecture

Each Java application runs inside a runtime instance of some concrete implementation of the abstract specification of the  Java virtual machine . There are three notions of JVM: specification, implementation, and instance.

  1. Specification : A document that describes what is required of JVM Implementation.
  2. Implementation: Known as JRE(Java Run Time Environment.)
  3. Instance: Whenever you will run a java class file an instance of JVM is created.

As shown in picture, JVM is divided into three main subsystems:

  1. Class Loader Subsystem
  2. Runtime Data Area
  3. Execution Engine

Class Loader Subsystem

The  Java virtual machine has a flexible  Class Loader architecture that allows a Java application to load classes in custom ways. In a JVM, each and every class is loaded by some instance of a  java.lang.ClassLoader . A classloader is a special Java class file that is responsible for loading other classes onto a Java Virtual Machine. If a Java class is invoked and needs to be executed on a Java Virtual Machine, a special Java component, called a  classloader , is used to find the Java class of interest, pull that Java class off of the file system, and execute the  bytecode of that class file on the Java Virtual Machine.

Java  Class Loader Subsystem loads, links and initializes the class file when it refers to a class for the first time at runtime. It is responsible for loading class files from file system, network or any other source. There are three default class loader used in Java, Bootstrap ,  Extension and  System or Application class loader.

Boot Strap class Loader

When a JVM starts up, a special chunk of machine code runs that loads the system classloader. This machine code is known as the  Bootstrap / Primordial classloader. It is platform specific machine instructions that kick off the whole classloading process. The bootstrap classloader also takes care of loading all of the code needed to support the basic  Java Runtime Environment (JRE), including classes in the java.util and the java.lang packages .

Extension ClassLoader

The Extension class loader loads the classes from the JRE’s extension directories, such  lib/ext directories. Extension ClassLoader delegates class loading request to its parent, Bootstrap and if unsuccessful, loads class form  jre/lib/ext directory or any other directory pointed by  java.ext.dirs system property. Extension ClassLoader in JVM is implemented by  sun.misc.Launcher$ExtClassLoader .

System/Application Class Loader

System/Application  Class Loader is responsible for loading Application Level Classpath, path mentioned  Environment Variable etc.

Classloader - Linking

Linking is the process of incorporating the loaded bytecodes into the Java  Runtime System so that the loaded Type can be used by the JVM. It involves verifying and preparing that class or interface, its direct superclass, its direct  superinterfaces , and its element type (if it is an array type), if necessary.

  1. Verify: Bytecode verifier will verify whether the generated bytecode is proper or not if verification fails we will get verification error
  2. Prepare: For all static variables memory will be allocated and assigned with default values.
  3. Resolve: All symbolic memory references are replaced with the original references from Method Area.

Initialization

This is the final phase of Class Loading, here all static variable will be assigned with the original values and  static block will be executed.

Runtime Data Areas

The  Java Virtual Machine (JVM) defines various run-time data areas that are used during execution of a program. Some of these data areas are created on Java Virtual Machine start-up and are destroyed only when the Java Virtual Machine exits. Other data areas are per  thread . Per-thread data areas are created when a thread is created and destroyed when the thread exits.

Method Area

It is memory which is shared among all Threads like  Heap . It is created on Java Virtual Machine startup. It contains the code actually a compiled code, methods and its data and fields. Runtime constant pool is also a part of  Method Area .

Heap Area

Heap is a  memory place where the objects and its instance variable are stored. Each time an object is created in Java it goes into the area of  memory known as heap.

Stack Area

Stack is a memory place where the methods and the  local variables are stored. Variable references (either primitive or object references) are stored in the  Stack

PC Register

PC Register basically is a  address of current instruction is being executed. Since each thread some sets of method which is going to be executed depends upon  PC Register. It has some value for each instruction and undefined for  native methods . It is usually for keep tracking of instructions.

Native Method Stack

Native methods are those which are written in languages other than java.  JVM implementations cannot load native methods and can‘t rely on conventional stacks . It is also associated with each thread. In short it same as stack but it is used for  native methods .

Execution Engine

This is the core of the JVM.  Execution engine can communicate with various memory areas of JVM. Each thread of a running Java application is a distinct instance of the virtual machine‘s execution engine. The byte code that is assigned to the runtime data areas in the JVM via  class loader is executed by the execution engine.

  1. Interpreter
  2. JIT Compiler
  3. Garbage Collector

Interpreter

Reads, interprets and executes the  bytecode instructions one by one. As it interprets and executes instructions one by one, it can quickly interpret one bytecode, but slowly executes the interpreted result. This is the disadvantage of the interpret language. The ‘language‘ called Bytecode basically runs like an  interpreter .

JIT Compiler

The  JIT compiler converts the bytecode to an intermediate-level expression, IR (Intermediate Representation), to execute  optimization , and then converts the expression to native code. The JIT compiler has been introduced to compensate for the disadvantages of the interpreter. The main purpose of JIT compiler is to improve the performance. Internally  JIT compiler maintains a separate count for every method. Whenever JVM across any method call, first that method will be  interpretednormally by the interpreter and JIT compiler increments the corresponding count variable.

Garbage Collector

Garbage collection (GC) is the process that aims to free up  occupied memory that is no longer referenced by any reachable Java object, and is an essential part of the Java virtual machine‘s (JVM‘s)  dynamic memory management system. All Java objects automatically grab the memory that they need when they are created, and when the object is no longer need, the Java  Garbage Collection process reclaim the memory. That means, the Garbage Collector tracked live objects and everything else designated garbage. More about.... Java Garbage Collection Basics

Native Method Interface

Native methods allow you to use code from other languages such as  C or C++ in your java code. You use them when java doesn‘t provide the functionality that you need.

Native Method Libraries

Native Method Libraries are Collection of the  Native Libraries which is required for the  Execution Engine .

http://net-informations.com/java/intro/jvm.htm

原文地址:https://www.cnblogs.com/feng9exe/p/12010329.html

时间: 2024-10-31 00:50:00

What is Java virtual machine?的相关文章

【cl】解决Fail to create the java Virtual Machine

eclipse打开,提示Fail to create the java Virtual Machine 解决方法: 1.到eclipse安装目录下,找到eclipse.ini 2.按键盘ctrl+F,复制以下字符“launcher.XXMaxPermSize”,去掉引号,查找 3.将随后出现的两个216m改成128m

启动Myeclipse报错“Failed to create the Java Virtual Machine”的解决办法

我安装的是Myeclipse 10.7.1.装上好久没用,今天启动突然报错:Failed to create the Java Virtual Machine. 检查Myeclipse安装好使用时好的啊,近期也没用,可能是近期升级了本地单独安装的jre版本导致的吧(Myeclipse使用自己的jre的). 整理了如下2个解决办法,可以选择一个使用,我选择的是第2个.经测试都ok. 方法一: 找到Myeclpise路径下的myeclipse.ini文件: 编辑将Xmx(JVM Heap最大允许的尺

解决Eclipse启动报错Failed to create the Java Virtual Machine

电脑:2G内存,WIN7 32位. 启动adt-bundle-windows-x86-20140702\eclipse\eclipse.exe时,报错[Failed to create the Java Virtual Machine].点确定后,Eclipse自动关闭 解决方法如下: 修改Eclipse目录下的配置文件[eclipse.iniadt-bundle-windows-x86-20140702\eclipse\eclipse.ini] 将下面两个值改为原来的0.5倍,然后保存,重新启

Unrecognized option: -jrockit Error: Could not create the Java Virtual Machine.

Unrecognized option: -jrockit Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit 是weblogic 10.3.5 启动的时候抛出的一个错误 weblogic安装时选择的是开发模式,选用的jdk是jdk1.7,domain域创建的时候选择的jdk也是jdk1.7 在 %DOMAIN_HOME%\bin\st

解决Fail to create the java Virtual Machine

在使用eclipse进行java编程或者安卓编程的时候,会偶尔出现这样的问题:Fail to create the java Virtual Machine.出现这种提示的愿意有很多,但终究还是eclipse本身的一个小问题 -------------------------------------------------------------- 技术qq交流群:JavaDream:251572072 ----------------------------------------------

eclipse faild to creat the java Virtual Machine的解决办法

打开eclipse的时候突然出现了 faild to creat the java Virtual Machine 解决办法:打开解压后的Eclipse文件夹,找到eclipse.ini配置文件 打开该文件,以下为用nodepad++打开为例 按照以上提示修改,然后重启eclipse即可. 原因:本机内存太小了,不够用,eclipse方法区(Perm Gen)用不了那么多,256M,设128就够了,这样eclipse觉得能保证MaxPerm为128的设置,所以可顺利启动eclipse.但是如果设

eclipse启动时弹出Failed to create the Java Virtual Machine

eclipse启动时弹出Failed to create the Java Virtual Machine 一.现象 今天装eclipse的时候出现Failed to create the Java Virtual Machine 的错误. 错误图片如下: 二.出错原因 把错误提示翻译一下,就是"无法创建java虚拟机",其实这也就是说明: 我的eclipse版本和虚拟机不兼容 三.查找错误 而我们知道,eclipse用的是系统的Java运行环境,也就是用的我们自己的JAVA虚拟机,也

A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. No Java virtual machine was found after searching the following locations: /usr/local/eclipse/

linux系统下jdk是已经安装好的情况之下软件出现 A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. No Java virtual machine was found after searching the following locations: /usr/local/eclipse/jre/bin/java java in yo

“failed to create java virtual machine” 解决方案

有时候我们在开发的过程当中,打开Eclipse会出现"failed to create java virtual machine"这样的错误提示: 当我们遇到这种错误提示时,可以按照以下方法解决: 首先找到eclipse安装文件夹,文件夹中有一个名叫eclipse.ini的配置文件,我们用记事本或editplus之类的编辑工具打开它: 接着我们将下图中红色框线部分的值全部改成128M,再次启动Eclipse即可: 希望以上经验能够帮助到大家.

Unable to find a java Virtual Machine-64位Oracle11g

Unable to find a java Virtual Machine--安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径"C:\app\用户名\product\11.2.0\dbhome_1\jdk"却弹出错误信息: --------------------------Unable to find a