溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

發(fā)布時(shí)間:2021-09-30 15:42:59 來(lái)源:億速云 閱讀:162 作者:柒染 欄目:web開(kāi)發(fā)

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

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開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

The Snapshot1 is generated.

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制

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

看完上述內(nèi)容,你們掌握如何使用Chrome開(kāi)發(fā)者工具研究JavaScript的垃圾回收機(jī)制的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI