generated from just-the-docs/just-the-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
layout: default | ||
title: JVM垃圾回收 | ||
parent: Java | ||
nav_order: 3 | ||
--- | ||
|
||
# 方法区 | ||
|
||
关注类的信息,如何被回收,3个条件: | ||
|
||
- 1 类的所有实例,及子类实例被回收 | ||
- 2 类的加载器被回收 | ||
- 3 Class对象没有被任何数据引用 | ||
|
||
每个jps文件都有一个唯一的类加载器。 | ||
|
||
# 堆 | ||
|
||
## 引用计数与可达性分析 | ||
|
||
- 引用计数在循环引用时,会出现无法回收的情况 | ||
|
||
### 可达性分析 | ||
|
||
根对象与普通对象,与根对象没有关联时,就被回收 | ||
|
||
- 线程Thread对象,引用栈帧中方法参数、局部变量等 | ||
- 系统类加载器的java.lang.Class对象(某个类的元信息) | ||
- 监视器对象,synchronized持有的对象 | ||
- 本地方法调用时使用的全局对象 | ||
|
||
# 引用 | ||
|
||
- 软引用:常用于缓存中,当内存不足时,使用软引用 SoftReference,回收可以放在一个软引用队列中,ReferenceQueue | ||
- 虚引用:不能通过引用去获取饮食的对象,唯一用途是内存释放时,可以收到一个通知。PhantomReference | ||
- 弱引用:不管GC时内存够不够,都会被回收,WeakReference,一般用于ThreadLocal中 | ||
- 终结引用:对象需要被回收时,会将关联的对象放在一个finalize队列中,第二次回收时,才会真正回收,但是真正回收前 | ||
,可以关联对象再关联对一个强引用,又不会回收。 | ||
|
||
# 垃圾回收算法 | ||
|
||
垃圾回收主要做两件事:标识是否被回收; 回收内存,释放空间。 | ||
|
||
GC线程进行垃圾回收,会影响到用户线程,会存在 **STW(Stop The World)** 时间。 | ||
|
||
可以从最大暂停时间、吞吐量、堆使用效率等方面考查垃圾回收算法的优劣。 | ||
|
||
## 标记-清除 Mark-Sweep GC | ||
|
||
标记阶段:用可达性分析找到存在对象 | ||
清除阶段:将不可达的对象回收 | ||
|
||
- 优点:实现简单 | ||
- 缺点: | ||
- 内存碎片多 | ||
- 分配速度慢,因为碎片多,在分配新内存时,要遍历所有碎片,找出合适的空闲内存 | ||
|
||
## 复制 Copy GC | ||
|
||
将堆分为From和To空间 | ||
GC开始,将GC root对象复制到To空间 | ||
把GC root关联的对象复制到To空间 | ||
From中剩下的数据,就是要被清除的 | ||
|
||
- 优点:吞吐量高,没有过大的碎片 | ||
- 缺点:内存使用率不高,只有一半的空间可以用来创建新对象 | ||
|
||
## 标记-整理 Mark-Compact GC | ||
标记阶段:用可达性分析找到存在对象 | ||
整理阶段:将存活对象整理到堆的一端,清理出存在对象原来的空间。 | ||
|
||
- 优点:内存使用率高,没有碎片 | ||
- 缺点:对堆中的对象搜索2次,效率不高 | ||
|
||
|
||
## 分代GC Generate GC | ||
将堆分为老年代与年轻化,年轻化分布Eden区和Survivor1、Survivor2区,年轻代比例8:1:1。 | ||
|
||
年轻代存放使用时间短的对象,比如每次创建订单的订单明细数据 | ||
|
||
老年代存入使用时间长的对象。比如Spring中的Bean | ||
|
||
- YGC 将年轻代的数据,GC次数达到15次,会被移入老年代 | ||
- FGC 整个堆回收 | ||
|
||
# 垃圾回收器 | ||
|
||
![img.png](img/gc_tool.png) | ||
|
||
## 年轻代-Serial | ||
|
||
![img.png](img/gc_serial.png) | ||
|
||
|
||
## 年轻代-ParNew | ||
|
||
![img.png](img/gc_parnew.png) | ||
|
||
|
||
## 老年代-CMS | ||
![img.png](img/gc_cms.png) | ||
|
||
- 初始标记 | ||
- 并发标记 | ||
- 重新标记:并发标记后漏标、误标、,查漏补缺 | ||
- 并发清理 | ||
|
||
## Parallel Scavenge | ||
|
||
![img.png](img/gc_parallel.png) | ||
|
||
## G1 | ||
- 分为Eden, Survivor, old | ||
- 默认分为2048个区域 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
layout: default | ||
title: 方法区 | ||
parent: Java | ||
nav_order: 3 | ||
--- | ||
|
||
|
||
方法区存在的数据 | ||
- 类的元信息(方法,虚方法表,字段,基本信息,常量),虚方法表是用于多态的实现 | ||
- 运行时常量池 | ||
- 字符串常量池(StringTable hotspot版本的jdk是在堆中) | ||
|
||
![img.png](img/matesapce_string_table_add.png) | ||
|
||
--- | ||
|
||
![img.png](img/matesapce_string_table_add1.png) | ||
|
||
jdk7 类的元信息在堆中永久代 | ||
jdk8 类的元信息,在内存中,叫元空间 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
layout: default | ||
title: 直接内存 | ||
parent: Java | ||
nav_order: 3 | ||
--- | ||
|
||
|
||
# 存在的意义 | ||
- 需要直接调用操作系统的功能 | ||
- 在网络编码时,使用NIO的方法,需要直接读写内存,不通过jvm的堆 |
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.