The Java® Virtual Machine Specification 笔记(一)

分类: JVM 发布于:

Data types

The Java Virtual Machine expects that nearly all type checking is done prior to run time, typically by a compiler, and does not have to be done by the Java Virtual Machine itself.

Java 虚拟机默认类型检查在编译阶段已经完成了。

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).

JVM支持的数据类型包括 一般数字类型,布尔型,和返回地址

一般数字类型包括:

  • byte: 8位有符号数(-127,128),默认为零。

  • short: 16有符号数, int: 32 length, float, double

  • char: 16位无符号数

Run-Time Data Areas

The pc Register

If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java Virtual Machine’s pc register is undefined. The Java Virtual Machine’s pc register is wide enough to hold a returnAddress or a native pointer on the specific platform

如果不是Native函数(是java methods), pc计数器中是当前指令的地址。 如果是native函数,undined

Java Virtual Machine Stacks

创建时机: stack 在线程被创建的时候被created。 stack中包含Stack frame。

it holds local variables and partial results, and plays a part in method invocation and return. Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

  • If the computation in a thread requires a larger Java Virtual Machine stack than is permitted, the Java Virtual Machine throws a StackOverflowError.

  • If Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

Heap

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

堆是被所有线程共享的。 作为运行时区域,这个地方的数据主要存放了 类实例 and 数组

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector);

堆被GC管理的,objects不会被显性的释放。

Method area 方法区(这个翻译不是很好)

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the “text” segment in an operating system process. 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.

Run-Time Constant Pool

A run-time constant pool is a per-class or per-interface run-time representation of the constant_pool table in a class file

Native Method Stacks

The following exceptional conditions are associated with native method stacks:

  • If the computation in a thread requires a larger native method stack than is permitted, the Java Virtual Machine throws a StackOverflowError.
  • If native method stacks can be dynamically expanded and native method stack expansion is attempted but insufficient memory can be made available, or if insufficient memory can be made available to create the initial native method stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

Frames

A new frame is created each time a method is invoked.

方法调用时,创建Frame

Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the runtime constant pool (§2.5.5) of the class of the current method.

Local Variables

Each frame (§2.6) contains an array of variables known as its local variables. The length of the local variable array of a frame is determined at compile-time and supplied in the binary representation of a class or interface along with the code for the method associated with the frame (§4.7.3).

A single local variable can hold a value of type boolean, byte, char, short, int, float, reference, or returnAddress. A pair of local variables can hold a value of type long or double.

Operand Stacks

Each frame (§2.6) contains a last-in-first-out (LIFO) stack known as its operand stack. The maximum depth of the operand stack of a frame is determined at compile-time and is supplied along with the code for the method associated with the frame (§4.7.3).

Dynamic Linking

TODO

Normal Method Invocation Completion

函数调用退出包含2个可能: 正常退出,异常退出

The current frame (§2.6) is used in this case to restore the state of the invoker, including its local variables and operand stack, with the program counter of the invoker appropriately incremented to skip past the method invocation instruction. Execution then continues normally in the invoking method’s frame with the returned value (if any) pushed onto the operand stack of that frame.

退出时,退出函数负责还原调用函数的现场。

Floating-Point Arithmetic 浮点运算

The Java Virtual Machine does not support either the IEEE 754 single extended or double extended format, except insofar as the double and double-extendedexponent value sets may be said to support the single extended format.

Exceptions

TODO

Type Conversion Instructions

类型转换指令

The Java Virtual Machine also directly supports the following narrowing numeric conversions: • int to byte, short, or char • long to int • float to int or long • double to int, long, or float

Object Creation and Manipulation

  • Create a new class instance: new.

  • Create a new array: newarray, anewarray, multianewarray.

  • Access fields of classes (static fields, known as class variables) and fields of class instances (non-static fields, known as instance variables): getstatic,putstatic, getfield, putfield.

  • Load an array component onto the operand stack: baload, caload, saload, iaload, laload, faload, daload, aaload.

  • Store a value from the operand stack as an array component: bastore, castore, sastore, iastore, lastore, fastore, dastore, aastore.

  • Get the length of array: arraylength.

  • Check properties of class instances or arrays: instanceof, checkcast.

Control Transfer Instructions

Conditional branch: ifeq, ifne, iflt, ifle, ifgt, ifge, ifnull, ifnonnull, if_icmpeq, if_icmpne, if_icmplt, if_icmple, if_icmpgt if_icmpge, if_acmpeq, if_acmpne.

Compound conditional branch: tableswitch, lookupswitch

Unconditional branch: goto, goto_w, jsr, jsr_w, ret.

Method Invocation and Return Instructions

Method-level synchronization is performed implicitly, as part of method invocation and return (§2.11.8). A synchronized method is distinguished in the run-time constant pool’s method_info structure (§4.6) by the ACC_SYNCHRONIZED flag, which is checked by the method invocation instructions. When invoking a method for which ACC_SYNCHRONIZED is set, the executing thread enters a monitor, invokes the method itself, and exits the monitor whether the method invocation completes normally or abruptly.

During the time the executing thread owns the monitor, no other thread may enter it.

If an exception is thrown during invocation of the synchronized method and the synchronized method does not handle the exception, the monitor for the method is automatically exited before the exception is rethrown out of the synchronized method.

Class Libraries

Classes that might require special support from the Java Virtual Machine include those that support: • Reflection, such as the classes in the package java.lang.reflect and the class Class. • Loading and creation of a class or interface. The most obvious example is the class ClassLoader. • Linking and initialization of a class or interface. The example classes cited above fall into this category as well. • Security, such as the classes in the package java.security and other classes such as SecurityManager. • Multithreading, such as the class Thread. • Weak references, such as the classes in the package java.lang.ref