前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Chrome开发者工具研究JavaScript的垃圾回收机制

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

作者头像
Jerry Wang
发布2020-05-06 21:26:42
3660
发布2020-05-06 21:26:42
举报

I use the following simple JavaScript code to illustrate:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档