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

[color=red][b]Java虚拟机内存分配模型[/b][/color]

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

[url]http://blog.jamesdbloom.com/JVMInternals.html[/url][quote][img]http://blog.jamesdbloom.com/images_2013_11_17_17_56/JVM_Internal_Architecture.png[/img][/quote]

Java Virtual Machine Specification - Runtime Data Areas:
[url]http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5[/url][quote][color=red][b]The pc Register[/b][/color]
Each Java virtual machine thread has its own pc (program counter) register.
[color=red][b]Java Virtual Machine Stacks[/b][/color]
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实例,存放:局部变量 / 方法参数 / 中间计算结果
[color=red][b]Heap[/b][/color]
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 ..?
[color=red][b]Method Area[/b][/color]
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 ..?
[color=red][b]Runtime Constant Pool[/b][/color]
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 中。
[color=red][b]Native Method Stacks[/b][/color][/quote]Java内存模型:
[url]http://blog.csdn.net/silentbalanceyh/article/details/4661230[/url]


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


关于java的内存分配,网上有种提法说分heap/stack/data segment/code segment。如下:
[url]http://wenku.baidu.com/view/efa5a3d9a58da0116c174988.html[/url]
[img]http://www.iteye.com/upload/attachment/130821/1692ed03-5136-337d-90af-e5ad8554bb7a.png[/img]
不知道这是谁发明的,不过这种提法是不正确的。stack和heap java中是有没错,但data segment 和 code segment,是C语言内存分配中的概念,也是大学组成原理课里的概念,在java世界里,没这种说法!
参考:[url]http://www.coderanch.com/t/392047/java/java/Code-Data-segment[/url][quote]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.[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值