Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

update jvm memory diagrams #34

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/01-jvm-memory-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ Java 中任何一个普通方法都具备虚函数的特征(运行期确认,

堆是用来存放对象的内存空间,`几乎`所有的对象都存储在堆中。

![jvm-memory](https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/jvm@main/images/jvm-memory.png)



### 堆的特点

- 线程共享,整个 Java 虚拟机只有一个堆,所有的线程都访问同一个堆。而程序计数器、Java 虚拟机栈、本地方法栈都是一个线程对应一个。
Expand Down Expand Up @@ -261,6 +265,8 @@ Java 虚拟机规范中定义方法区是堆的一个逻辑部分。方法区存

当类被 Java 虚拟机加载后, .class 文件中的常量就存放在方法区的运行时常量池中。而且在运行期间,可以向常量池中添加新的常量。如 String 类的 `intern()` 方法就能在运行期间向常量池中添加字符串常量。

![jvm-runtime-constant-pool](https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/jvm@main/images/jvm-runtime-constant-pool.png)

## 直接内存(堆外内存)

直接内存是除 Java 虚拟机之外的内存,但也可能被 Java 使用。
Expand All @@ -279,3 +285,7 @@ Java 虚拟机规范中定义方法区是堆的一个逻辑部分。方法区存
- 堆内存作用链:本地 IO -> 直接内存 -> 非直接内存 -> 直接内存 -> 本地 IO

> 服务器管理员在配置虚拟机参数时,会根据实际内存设置`-Xmx`等参数信息,但经常忽略直接内存,使得各个内存区域总和大于物理内存限制,从而导致动态扩展时出现`OutOfMemoryError`异常。



> ![jvm-off-heap-memory](https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/jvm@main/images/jvm-off-heap-memory.png)
4 changes: 4 additions & 0 deletions docs/02-hotspot-jvm-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ HotSpot VM 的自动内存管理系统要求对象的大小必须是 8 字节的

## 对象的创建过程

![new-instruction](https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/gh/doocs/jvm@main/images/new-instruction.png)

### 类加载检查

虚拟机在解析`.class`文件时,若遇到一条 new 指令,首先它会去检查常量池中是否有这个类的符号引用,并且检查这个符号引用所代表的类是否已被加载、解析和初始化过。如果没有,那么必须先执行相应的类加载过程。
Expand All @@ -57,6 +59,8 @@ HotSpot VM 的自动内存管理系统要求对象的大小必须是 8 字节的

至此,整个对象的创建过程就完成了。



## 对象的访问方式

所有对象的存储空间都是在堆中分配的,但是这个对象的引用却是在堆栈中分配的。也就是说在建立一个对象时两个地方都分配内存,在堆中分配的内存实际建立这个对象,而在堆栈中分配的内存只是一个指向这个堆对象的指针(引用)而已。 那么根据引用存放的地址类型的不同,对象有不同的访问方式。
Expand Down
Binary file added images/jvm-memory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/jvm-off-heap-memory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/jvm-runtime-constant-pool.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/new-instruction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.