問題描述
在尋找 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.
推薦答案
在 DevTools
的 Profiles
選項卡中選擇 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)
的一個元素,查看顯示的Distance
、Shallow Size
、Retained 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
.檢查相應的 Distance
、Shallow Size
和 Retained 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 中觸發垃圾收集的內存分配?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!