您的位置:首页 > 其它

[JVM之旅]JVM运行时数据区域(Run-Time Data Areas)

2014-11-29 15:35 417 查看

唠叨一下

根据最新的JVM规范粗翻了一下要点。其实知道各个分区的作用是次要的,关键的是要知道JVM的设计思想。
看过相关JVM书籍的人都能大概说出运行时数据区的划分,堆是存放实例的,虚拟机栈存放着方法运行时所需要的数据结构,是否线程私有,会抛出何种异常等等。功底扎实一点的或许能说出栈、堆是何物。
其实这些都不太重要,或者说不是关键吧,花时间去看了总会看懂。然而思考才是更重要的,我们要去想为什么运行时数据区会是这样划分(说到这个,我认为,有一点汇编基础会更好理解)。

其实主要要搞清楚的就几个问题:

堆和栈的特性(数据结构角度);
JVM中堆与栈的作用,为什么要实例存放于堆,而不是栈;
不同的区其设定为线程私有与线程共享的原因
异常抛出的情况。

最好懂点汇编,知道代码区,数据区。



Program Counter Register 程序计数器

Each Java Virtual Machine thread has its own pc (program counter) register.
程序计数器为线程私有的

If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed.
当前方法为非native时,程序计数器会存储正在执行的命令的地址

If the method currently being executed by the thread is native, the value of the Java Virtual Machine's pc register is undefined.
当前方法为native时,程序计数器的值为undefined

JVM Stacks 虚拟机栈

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread.
JVM stacks 也是线程私有的,和线程同时创建

A Java Virtual Machine stack stores frames.
JVM stack中储存着栈帧(Frames)

Frames may be heap allocated.
栈帧有可能分配在堆中,因为JVM stack并不是对栈帧 直接 进行操作

The memory for a Java Virtual Machine stack does not need to be contiguous.
JVM stacks 在内存分布上可能是不连续的

This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation.
JVM规范允许JVM stacks的大小既可以是固定的也可以是动态分配的

Heap 堆

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads.

Heap是线程共享的

The heap is the run-time data area from which memory for all class instances and arrays is allocated.

类实例与数组分配在堆中

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); objects are never explicitly deallocated.

堆,存储着那些被自动存储管理系统(例如GC)所管理的对象,而对象不会被显式释放。

The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

与栈相似,堆并不要求在内存上是连续的,并且可以固定或者动态改变大小

Method Area 方法区

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads.

Method Area 是线程共享的

The method area is analogous to the storage area for compiled code of a conventional language.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 used in class and instance initialization and interface initialization.

Method Area 存储着每个类的结构,例如运行时常量池,域和方法的数据,方法与构造器等代码数据,

The method area is created on virtual machine start-up.

Method Area 创建于虚拟机启动时

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

虽然 Method Area 被描述为Heap的逻辑部分,但是根据具体的JVM实现可以选择不对这个区进行垃圾回收

The method area may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger method area becomes unnecessary. The memory for the method area does not need to be contiguous.

同样的Method Area并不要求在内存上是连续的,并且可以固定或者动态改变大小

Run-Time Constant Pool 运行时常量池

Each run-time constant pool is allocated from the Java Virtual Machine's method area

运行时常量池 被分配在方法区中

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

运行时常量池 代表着在运行时每个类或者接口的字节码文件中的常量表

It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time.

运行时常量池 用于存放编译时产生的各种字面量和符号引用

The run-time constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.

它的功能类似于传统编程语言的符号表,虽然它包含的数据要丰富的多。

Native Method Stacks

An implementation of the Java Virtual Machine may use conventional stacks, colloquially called "C stacks," to support native methods (methods written in a language other than the Java programming language).

就是用来支持非Java方法的调用,并不是所有JVM都有实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: