虚拟机规范学习

地址https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html

2.1. The class File Format

Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format.


2.2. Data Types

Like the Java programming language, the Java Virtual Machine operates on two kinds of types: primitive types and reference types. There are, correspondingly, two kinds of values that can be stored in variables, passed as arguments, returned by methods, and operated upon: primitive values and reference values.


2.3. Primitive Types and Values

The primitive data types supported by the Java Virtual Machine are the numeric types, the boolean type (§2.3.4), and the returnAddress type (§2.3.3).

The numeric types consist of the integral types (§2.3.1) and the floating-point types (§2.3.2).

The integral types are:

  • byte, whose values are 8-bit signed two's-complement integers, and whose default value is zero

  • short, whose values are 16-bit signed two's-complement integers, and whose default value is zero

  • int, whose values are 32-bit signed two's-complement integers, and whose default value is zero

  • long, whose values are 64-bit signed two's-complement integers, and whose default value is zer

  • char, whose values are 16-bit unsigned integers representing Unicode code points in the Basic Multilingual Plane, encoded with UTF-16, and whose default value is the null code point ('\u0000')

The floating-point types are:

  • float, whose values are elements of the float value set or, where supported, the float-extended-exponent value set, and whose default value is positive zero

  • double, whose values are elements of the double value set or, where supported, the double-extended-exponent value set, and whose default value is positive zero

The values of the boolean type encode the truth values true and false, and the default value is false.


2.3.1. Integral Types and Values

The values of the integral types of the Java Virtual Machine are:

  • For byte, from -128 to 127 (-27 to 27 - 1), inclusive

  • For short, from -32768 to 32767 (-215 to 215 - 1), inclusive

  • For int, from -2147483648 to 2147483647 (-231 to 231 - 1), inclusive

  • For long, from -9223372036854775808 to 9223372036854775807 (-263 to 263 - 1), inclusive

  • For char, from 0 to 65535 inclusive

2.3.3 returnAddress类型和值

returnAddress类型会被Java虚拟机的jsr、ret和jsr_w指令所使用。return

Address类型的值指向一条虚拟机指令的操作码。与前面介绍的那些数值类的原生类型不同,returnAddress类型在Java语言之中并不存在相应的类型,也无法在程序运行期间更改returnAddress类型的值。

Run-Time Data Areas

The Java Virtual Machine 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.

2.5.1. The pc Register

jvm同时可支持多个线程运行,每个线程都拥有自己的程序计数器。计数器中包含线程即将执行的jvm指令地址。当然如果方法为native方法,地址为undefined


2.5.2. Java Virtual Machine Stacks

每个jvm线程都有jvm stack,跟线程一起创建,主要存储frames。

jvm stack主要存储local variables and partial results, and plays a part in method invocation and return

本地变量和局部结果,并且完成方法调用和返回。

因为只能通过push和pop frames来操作jvm stack,?所以jvm stack 内存分配可以不是连续的。

jvm可以分配固定大小,或者动态分配。

如果是固定的内存,那么在线程中需要更多栈时就会导致StackOverflowError

如果可以动态分配,但是由于没有足够内存了,就被会导致OutofMemory

2.5.3. Heap

所有线程共享。分配给所有class instances和数组的运行时数据区。虚拟机一启动就创建。heap 由垃圾回收器回收。

heap可以固定大小或者由按需扩容。但扩容时内存不够时就会报OOM异常。不需要连续

2.5.4. Method Area

所有线程共享。It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (§2.9) used in class and instance initialization and interface initialization.

存储运行时常量池,field和方法数据。方法和构造器的代码。


Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it

方法区虽然逻辑上是heap的一部分,但是一般不会选择做垃圾收集或者压缩。如果不够也会报OOM,不需要连续。

2.5.5. Run-Time Constant Pool

是类或者接口在运行时的常量池。它包括好几种常量,从在编译阶段知道的数字,到运行阶段解析的方法和field引用。每一个运行时常量池都是从方法区分配的。当创建一个类或者接口时就会创建运行时常量池。当方法区内存不够时就会造成OutOfMemoryError


2.5.6. Native Method Stacks

存储本地方法。本地方法不是java语言写的。If supplied, native method stacks are typically allocated per thread when each thread is created.

可能会StackOutOfError和OOM.

猜你喜欢

转载自www.cnblogs.com/jianghaibo/p/9375060.html