使用Chrome开发者工具研究JavaScript的垃圾回收机制

简介:

I use the following simple JavaScript code to illustrate:

var JerryTestArray = [];
(function(){
      for( var i = 0; i < 100; i++){
        JerryTestArray[i] = document.createElement("div");
       }
})();

Create a new empty tab in your Chrome, and first create a snapshot with empty heap status by click “Take Snapshot” button:

The Snapshot1 is generated.

Now switch to tab Console, paste the JavaScript code and execute it in console.

And switch to Profiles tab again to make the second snapshot:

Once done, select the second snapshot, choose “Comparison” and “Snapshot1” as filter:

We can find out from column “New” that 100 div nodes are created as we expect.

Since these nodes are not appended to document node so they are invisible to end user, so displayed as “Detached DOM”. The JerryTestArray still holds the reference to each div node so Garbage collector will not touch these nodes.

In order to make Garbage collector recycle the memory occupied by these nodes, just assign another value to JerryTestArray in console:

Once done, make the third snapshot and compare it with the second. Now we can find that the re-assignment to JerryTestArray will trigger the destruction of those 100 div nodes by Garbage collector:

Meanwhile, the string we use in assignment could also be inspected via the combination of filters below:

There is another kind of profile in Chrome development tool which can give you an overview about timeline of memory allocation:

Click Start button in above screenshot, and paste the following code in console and executed:

var JerryTestArray = [];
(function(){
      for( var i = 0; i < 98; i++){
        JerryTestArray[i] = document.createElement("span");
        JerryTestArray[i].className = "JerryClassName" + i;
       }
})();

After the code is executed, paste the following code and execute:

JerryTestArray[30] = "this is a long test............................end";

Now stop the profile. The profile is displayed as below. The highlighted vertical blue line indicates the timeslot when the 97 Span elements are created. Note that the number of Span elements displayed here is not 98 but 97 since Chrome development tool displays the final status of objects after “stop profile” button is clicked ( the reference to 30th Span element is replaced by String, so it is recycled by GC ).

You can drag the two vertical bars to define the time range between which you would like to inspect. For example the time range below contains the timeslot when the below assignment occurs:

JerryTestArray[30] = "this is a long test............................end";

With this gained knowledge now we can check the memory allocation and destruction in some real application. For example click tile “My Tasks” to enter this application, make the first snapshot and click back button within application to return to launchpad, and make the second snapshot and review the comparison result.

From the result we find out lots of stuff are deleted after we return to launchpad:

Hover your mouse to a given destructed object and you can review its detail:

For more tips How I use Chrome development tool in my daily work, please refer to this blog Chrome Development Tool tips used in my daily work

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
1月前
|
JavaScript 前端开发 算法
Java Script 中的垃圾回收机制有哪些缺点
Java Script 中的垃圾回收机制有哪些缺点
13 0
|
1月前
|
JavaScript 前端开发 算法
描述 JavaScript 中的垃圾回收机制。
描述 JavaScript 中的垃圾回收机制。
18 1
|
3月前
|
存储 JavaScript 前端开发
|
3月前
|
Web App开发 存储 JavaScript
【JavaScript】垃圾回收与内存泄漏
JavaScript的*垃圾回收机制*是一种自动化的内存管理机制,用于检测和回收不再使用的内存资源,以便重新分配给其他需要的部分。JavaScript中的垃圾回收器负责跟踪和管理内存的分配和释放,使开发人员无需手动管理内存。 *内存泄漏*指的是程序中分配的内存空间无法被释放和回收,并且随着时间推移导致可用内存逐渐减少。
52 0
|
3月前
|
Web App开发 数据采集 JavaScript
Chrome开发者工具探秘:元素面板的神奇魔法与实战解析
Chrome开发者工具探秘:元素面板的神奇魔法与实战解析
24 0
|
3月前
|
Web App开发 存储 缓存
Chrome 开发者工具 Network 里 Failed to load response data 提示消息的含义
Chrome 开发者工具 Network 里 Failed to load response data 提示消息的含义
142 0
|
8天前
|
JavaScript 前端开发 Java
js 垃圾回收机制的方法
js 垃圾回收机制的方法
|
1月前
|
JavaScript 前端开发 Java
JavaScript的垃圾回收机制
JavaScript的垃圾回收机制
39 1
|
1月前
|
Web App开发 前端开发 JavaScript
防止你的 Web 应用被别人通过 Chrome 开发者工具进行调试的一种简单办法
防止你的 Web 应用被别人通过 Chrome 开发者工具进行调试的一种简单办法
30 0
|
3月前
|
前端开发 JavaScript 算法
JavaScript 内存管理的秘密武器:垃圾回收(下)
JavaScript 内存管理的秘密武器:垃圾回收(下)
JavaScript 内存管理的秘密武器:垃圾回收(下)