iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >如何使用Chrome开发者工具研究JavaScript的垃圾回收机制
  • 263
分享到

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

2024-04-02 19:04:59 263人浏览 独家记忆
摘要

如何使用Chrome开发者工具研究javascript的垃圾回收机制,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。I use the foll

如何使用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:

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

The Snapshot1 is generated.

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

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

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

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

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

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

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

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

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

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:

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

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:

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

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

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

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

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

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 ).

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

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";

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

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.

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

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

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

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

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

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

看完上述内容,你们掌握如何使用Chrome开发者工具研究JavaScript的垃圾回收机制的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网html频道,感谢各位的阅读!

--结束END--

本文标题: 如何使用Chrome开发者工具研究JavaScript的垃圾回收机制

本文链接: https://www.lsjlt.com/news/82442.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何使用Chrome开发者工具研究JavaScript的垃圾回收机制
    如何使用Chrome开发者工具研究JavaScript的垃圾回收机制,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。I use the foll...
    99+
    2024-04-02
  • 如何使用Chrome开发者工具研究JavaScript里函数的原生实现
    本篇文章为大家展示了如何使用Chrome开发者工具研究JavaScript里函数的原生实现,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。As the size of...
    99+
    2024-04-02
  • 使用Go语言开发,如何合理管理内存和垃圾回收器
    在Go语言中,内存管理和垃圾回收是由Go运行时系统自动处理的,开发者一般不需要显式地管理内存。Go语言的垃圾回收器使用了标记-清除算...
    99+
    2023-10-08
    Golang
  • 如何正确的使用微信web开发者工具
    如何正确的使用微信web开发者工具?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。为帮助开发者更方便、更安全地开发和调试基于微信的网页,微信推出了 web 开发者工具。它是一个桌...
    99+
    2023-05-31
    java 开发者工具
  • 如何使用Git作为PHP开发的版本控制工具?
    Git是目前最流行的版本控制工具之一,广泛应用于各种编程语言和开发环境中。对于PHP开发者来说,Git是一个非常有用的工具,可以帮助他们更好地管理代码,并与其他开发者协同工作。本文将介绍如何使用Git作为PHP开发的版本控制工具。 一、Gi...
    99+
    2023-06-30
    http 关键字 git
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作