如何检测在 JavaScript 中触发垃圾收集的内存分配?

2023-05-13前端开发问题
3

本文介绍了如何检测在 JavaScript 中触发垃圾收集的内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在寻找 JavaScript 库(铆钉)中的性能问题时,我发现垃圾收集在一次运行中发生了 3 到 4 次,占用了大约 15% 的执行时间(使用 Chrome DevTools JS Profile).

While looking for performance issues in a JavaScript library (rivets), i found that garbage collection occurs three to four times in a run, taking ~15% of execution time (using Chrome DevTools JS Profile).

由于垃圾回收的原因,至少有 30 个地方创建了临时函数/对象作为潜在候选对象.

There are at least 30 places where temporary functions / objects are created being potential candidates for the reason of the garbage collection.

我想知道是否有办法找到负责分配被垃圾回收的内存的函数,这样我就可以专注于我的性能调整.

I'd like to know if there's a way to find what functions are responsible for the allocation of the memory being garbage collected, so i can focus my performance tuning.

我记录了堆分配时间线,但它没有区分被垃圾收集的内存并且仍然持有引用(没有像 DevTools 中指出的灰色条 doc)

I recorded Heap Allocation TimeLine but it does not differentiate memory that was garbage collected and that still holds a reference (there's no gray bar as pointed in DevTools doc)

还没有运气记录堆分配配置文件.

Also recorded Heap Allocation Profile without luck.

推荐答案

DevToolsProfiles 选项卡中选择 Record Heap Allocation.包装 javascript 应在对 setTimeout() 的调用中进行评估,持续时间设置为足够的时间,以便在函数传递给 之前单击 Start>setTimeout 被调用;例如

At Profiles tab at DevTools select Record Heap Allocation. Wrap javascript which should be evaluated within a call to setTimeout() with a duration set to enough time to click Start before function passed to setTimeout is called; for example

<!DOCTYPE html>
<html>
<head>
  <script>
    t = 5;
    setTimeout(function() {
      (function test1() {
        var a = 123;
        function abc() {
          return a
        }
        abc();
      }());
    }, 10000)
  </script>
</head>
<body></body>
</html>

setTimeout 被称为蓝条时,可能会在时间轴上出现一个灰条.单击 Ctr+E 停止记录堆配置文件.

When setTimeout is called a blue bar, possibly followed by a gray bar should appear at timeline. Click Ctr+E to stop recording heap profile.

在时间线图中选择蓝色或灰色条.在默认选项为 Summary 的下拉菜单中选择 Containment.选择

Select blue or gray bar at timeline graph. Select Containment at dropdown menu where default option is Summary. Select

[1] :: (GC roots) @n

其中 n 是一个数字.

通过单击[1] :: (GC 根) 左侧的三角形来展开选择.选择[1] :: (GC root)的一个元素,查看显示的DistanceShallow SizeRetained Size 用于选择的列.

Expand the selection by clicking triangle to left of [1] :: (GC roots). Select an element of [1] :: (GC roots), review the displayed Distance, Shallow Size, and Retained Size columns for the selection.

要查看特定功能,请滚动到

To check specific functions, scroll to

[2] :: (External strings) @n

到应该列出全局变量和函数调用的位置;例如,"t""setTimeout" 来自 javascrip.检查相应的 DistanceShallow SizeRetained Size 列以进行选择.

to where global variables and function calls should be listed; i.e.g., "t" and "setTimeout" from above javascrip. Check corresponding Distance, Shallow Size, and Retained Size columns for the selection.

这篇关于如何检测在 JavaScript 中触发垃圾收集的内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui laydate日期时间范围,时间默认设定为23:59:59
在Layui中,如果你想设置日期时间选择器(datetime)的默认结束时间为当天的23:59:59,你可以使用如下代码: laydate.render({ elem: '#test10' ,type: 'datetime' ,range: true ,max: '{:date("Y-m-d 23:59:59")}' ,ready: function(date){ $(".layui-laydat...
2024-10-24 前端开发问题
279

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125