JVM内存结构之二·堆区

2021-08-13

2.5 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.
The heap is created on virtual machine start-up.

jvms里并没有规定object的内存结构,所以应该是交给jvm实现者自定义的。

https://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html 这里有对象头的解释。

Common structure at the beginning of every GC-managed heap object. (Every oop points to an object header.) Includes fundamental information about the heap object's layout, type, GC state, synchronization state, and identity hash code. Consists of two words. In arrays it is immediately followed by a length field. Note that both Java objects and VM-internal objects have a common object header format.

 

2.5.0 对象的内存布局

 

存储对象基本信息的数据结构。如 类型(对象还是数组),同步状态,hash code等。


objects are never explicitly deallocated.??显示释放什么意思??代码中直接free的意思吗????

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

If a computation requires more heap than can be made available by the automatic storage management system, the Java Virtual Machine throws an OutOfMemoryError.

2.5.1 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 used in class and interface initialization and in instance initialization.

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

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

If memory in the method area cannot be made available to satisfy an allocation request, the Java Virtual Machine throws an OutOfMemoryError.

 

在jdk源码中,Klass(jdk/src/hotspot/share/oops/klass.hpp)定义了java class在内存中的数据结构。

方法区中存的就是一个个Klass实例

 

2.5.2 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 (§4.4)
Each run-time constant pool is allocated from the Java Virtual Machine's method area (§2.5.4). The run-time constant pool for a class or interface is constructed when the class or interface is created (§5.3) by the Java Virtual Machine.

When creating a class or interface, if the construction of the run-time constant pool requires more memory than can be made available in the method area of the Java Virtual Machine, the Java Virtual Machine throws an OutOfMemoryError.

 

[1]:深入理解JAVA虚拟机:JAVA高级特性与最佳实践 by 周志明