使用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:
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:
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 ).
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";
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
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703943/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JavaScript的垃圾回收機制JavaScript
- javascript 垃圾回收機制JavaScript
- javascript的垃圾回收機制指的是什麼?JavaScript
- javascript的垃圾回收機制指的是什麼JavaScript
- 使用Chrome開發者工具研究JavaScript裡函式的原生實現ChromeJavaScript函式
- javascript的垃圾回收機制和記憶體管理JavaScript記憶體
- chrome開發者工具的使用Chrome
- JS的垃圾回收機制JS
- jvm的垃圾回收機制JVM
- PHP的垃圾回收機制PHP
- chrome開發者工具使用Chrome
- java垃圾回收機制Java
- js垃圾回收機制JS
- Python垃圾回收機制Python
- JVM 垃圾回收機制JVM
- JVM垃圾回收機制JVM
- Java 垃圾回收機制Java
- PHP的垃圾回收機制-回收週期PHP
- 用垃圾回收機制解釋JavaScript中的閉包JavaScript
- Flutter中的垃圾回收機制Flutter
- 剖析垃圾回收機制(上)
- java垃圾回收機制整理Java
- 聊聊JVM的垃圾回收機制GCJVMGC
- 秒懂JVM的垃圾回收機制JVM
- Java的垃圾回收(Garbage Collection)機制Java
- JAVA垃圾回收機制和Python垃圾回收對比與分析JavaPython
- JS垃圾回收機制筆記JS筆記
- [效能][JVM]jvm垃圾回收機制JVM
- V8垃圾回收機制
- JVM垃圾回收機制入門JVM
- 談談 JVM 垃圾回收機制JVM
- 【翻譯】PHP 垃圾回收機制PHP
- 圖解Golang垃圾回收機制!圖解Golang
- JavaScript的垃圾收集機制JavaScript
- [譯] 通過垃圾回收機制理解 JavaScript 記憶體管理JavaScript記憶體
- javascript垃圾回收JavaScript
- 超詳細的node垃圾回收機制
- PHP的垃圾回收機制-引用計數PHP