Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nswbmw committed Apr 13, 2018
1 parent 1a64016 commit 7bd7b91
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions 2.2 heapdump.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ heapdump-100438986.797085.heapsnapshot
切换到 Summary 页,可以看到有如下 5 个属性:

1. Contructor:构造函数名,例如 Object、Module、Socket,(array)、(string)、(regexp) 等加了括号的分别代表内置的 Array、String 和 Regexp。
2. Distance:到 GC roots (GC 根对象)的距离。GC 根对象在浏览器中一般是 window 对象,在 Node.js 中是 global 对象距离越大,则说明引用越深,有必要重点关注一下,极有可能是内存泄漏的对象
2. Distance:到 GC roots (GC 根对象)的距离。GC 根对象在浏览器中一般是 window 对象,在 Node.js 中是 global 对象距离越大,则说明引用越深。
3. Objects Count:对象个数。
4. Shallow Size:对象自身的大小,不包括它引用的对象。
5. Retained Size:对象自身的大小和它引用的对象的大小,即该对象被 GC 之后所能回收的内存大小。

**小提示**: 一个对象的 Retained Size = 该对象的 Shallow Size + 该对象可直接或间接引用的对象的 Shallow Size 之和。Shallow Size == Retained Size 的有 (boolean)、(number)、(string),它们无法引用其他值,并且始终是叶子节点。
**小提示**一个对象的 Retained Size = 该对象的 Shallow Size + 该对象支配树上其子节点的 Retained Size 之和。Shallow Size == Retained Size 的有 (boolean)、(number)、(string),它们无法引用其他值,并且始终是叶子节点。

单击 Retained Size 选择降序展示,可以看到 (closure) 这一项引用的内容达到 98%,继续展开如下:

Expand Down
2 changes: 1 addition & 1 deletion 2.3 memwatch-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ memwatch 监听以下两个事件:
}
```

2. leak:内存泄露事件,触发该事件的条件是:内存在连续 5 次 GC 后都是增长的。打印如下:
2. leak:可疑的内存泄露事件,触发该事件的条件是:内存在连续 5 次 GC 后都是增长的。打印如下:

```js
{
Expand Down
4 changes: 2 additions & 2 deletions 3.6 Event Loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ poll 阶段主要有两个功能:

上面的 6 个阶段并没有出现 process.nextTick(),process.nextTick() 不在 Event Loop 的任何阶段执行,而是在各个阶段切换的中间执行,即从一个阶段切换到下个阶段前执行。这里还需要提一下 macrotask 和 microtask 的概念,macrotask(宏任务)指 Event Loop 每个阶段执行的任务,microtask(微任务)指每个阶段之间执行的任务。即上述 6 个阶段都属于 macrotask,process.nextTick() 属于 microtask。

**小提示**:Promise.then 也属于 microtask 的一种。
**小提示**process.nextTick() 的实现和 v8 的 microtask 并无关系,是 Node.js 层面的东西,应该说 process.nextTick() 的行为接近为 microtask。Promise.then 也属于 microtask 的一种。

最后,放出一张关于 Event Loop 非常直观的图:

Expand Down Expand Up @@ -255,7 +255,7 @@ TypeError: Chaining cycle detected for promise #<Promise>
at bootstrap_node.js:607:3
```

**解释**:promise.then 类似于 process.nextTick,都会将回调函数注册到 microtask 阶段。上面代码会导致死循环,类似前面提到的:
**解释**Promise A+ 的规范里规定 promise 不能返回自己。仔细想想,即使规范里不规定,promise.then 类似于 process.nextTick,都会将回调函数注册到 microtask 阶段。上面代码也会导致死循环,类似前面提到的:

```js
process.nextTick(function tick () {
Expand Down
6 changes: 5 additions & 1 deletion 4.5 supervisor-hot-reload.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ Starting child process with 'node app.js'
...
```

## 4.5.4 参考链接
## 4.5.4 内存泄露问题

这里需要声明一下,虽然修改 require.cache + Proxy 实现了我们想要的功能,但这样做存在内存泄漏问题,因为即使删除了一个模块的缓存,但父模块的缓存中还引用着旧的模块导出的对象。这个问题可以不用太关心,知道为什么就好,因为我们只是在开发环境使用 proxy-hot-reload。

## 4.5.5 参考链接

- https://nodejs.org/dist/latest-v8.x/docs/api/async_hooks.html

Expand Down

0 comments on commit 7bd7b91

Please sign in to comment.