未完 JVM Runtime Data Areas & Java Memory Model | 内存分配模型 & Java数据存储

Java虚拟机内存分配模型

需精读:Chapter 5 of Inside the Java Virtual Machine - The Java Virtual Machine:
http://www.artima.com/insidejvm/ed2/jvm2.html
引用
所有的对象都是分配在heap上的


http://blog.jamesdbloom.com/JVMInternals.html
引用


Java Virtual Machine Specification - Runtime Data Areas:
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5
引用
The pc Register
Each Java virtual machine thread has its own pc (program counter) register.
Java Virtual Machine Stacks
Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread. A Java virtual machine stack stores frames.
每个线程有各自独立的Stack实例,存放:局部变量 / 方法参数 / 中间计算结果
Heap
The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.
jvm中只有一个Heap实例,存放:实例变量 / 所有的对象  ....and ..?
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 runtime 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.
jvm中只有一个Method Area实例,存放:类变量 / information about loaded types(class,interface,etc)...and ..?
Runtime Constant Pool
A runtime constant pool is a per-class or per-interface runtime representation of the constant_pool table in a class file (§4.4). It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at runtime. The runtime 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.
Constant pool 位于 Method Area 中。
Native Method Stacks
Java内存模型:
http://blog.csdn.net/silentbalanceyh/article/details/4661230


Java Memory Model:
jls 17.4 - Memory Model:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4
The Java Memory Model:
http://www.cs.umd.edu/~pugh/java/memoryModel/index.html#reference
JSR-133: Java Memory Model and Thread Specification:
https://www.cs.umd.edu/users/pugh/java/memoryModel/jsr133.pdf



关于java的内存分配,网上有种提法说分heap/stack/data segment/code segment。如下:
http://wenku.baidu.com/view/efa5a3d9a58da0116c174988.html
未完 JVM Runtime Data Areas & Java Memory Model | 内存分配模型 & Java数据存储
不知道这是谁发明的,不过这种提法是不正确的。stack和heap java中是有没错,但data segment 和 code segment,是C语言内存分配中的概念,也是大学组成原理课里的概念,在java世界里,没这种说法!
参考: http://www.coderanch.com/t/392047/java/java/Code-Data-segment
引用
Q:
Can anyone help me knowing what role exactly code segment and data segment plays in general in Java?
A:
I am not exactly sure what you mean by "code segment" etc.
When a class is used in any manner, it is automatically "loaded" the first time by the JVM. This causes a Classfile to be created for it, all the code is loaded into the Method Area, and any static initializers are executed.
Methods, constructors and initializing code (both static and non-static) are kept in the Method Area of the Classfile. There is no "per instance" implementation of code. The classfile and it's methods stay until the class is unloaded. Most of the classes that you will use as a beginner are loaded by the bootstrap classloader and stay put until the JVM comes down.
Data that is related to a particular object of the class (fields) are kept in the object itself on the heap. So each object has a field that relates to each variable that defines it's current state. If it inherits variable from a Super class, the object has a place for that also (even if it is hidden by a variable with the same name in the subclass).
Static variables are kept in a central location related to the Classfile. These variables are "shared" by all instances of the class. Of course those variables may hold references to objects which are themselves on the heap.

你可能感兴趣的:(java,jvm,内存模型,数据分配)