目录

Analyzing Web Page Memory Using DevTools

Enabling DevTools

DevTools is a built-in tool of the Chrome browser. After you download and launch Chrome, press F12 or Shift+Ctrl+I on the page where memory analysis is needed to start DevTools.

Obtaining JS Heap Memory Snapshots

On the Memory panel, select Heap snapshot and click Take snapshot to capture a memory snapshot of the current page.

Analyzing Heap Snapshots

[h2]Summary

The Summary tab displays the overview of the current memory snapshot. Key details include:

A filter panel on the right side of the Summary tab allows you to focus on specific objects. For example, selecting "Objects allocated between Snapshot 2 and Snapshot 3" (as shown below) helps locate where memory issues occur.

[h2]Comparison

In Comparison, you can compare the current snapshot with another snapshot to track the changes of object properties and memory usage. Key details include:

You can analyze memory issues by comparing the differences between snapshots on the Comparison tab.

[h2]Containment

Containment provides a top-down tree view that allows you to browse and explore the contents of heap memory. You can use it to analyze the reference relationships of any variable.

[h2]Statistics

Statistics displays the memory usage ratio of different object types in a pie chart.

Memory Leak Analysis Process

  1. Open a page where memory leaks may occur and enable DevTools. The following figure shows a page from the memory-leak-simulation project on GitHub. This page simulates a memory leak scenario by setting a global array and continuously pushing the 'memory leak' string into it.

  2. Record user operations that may cause memory leaks on the Performance tab to identify the specific operations or components triggering leaks. As shown in the following figure, the web page has been loaded, but the memory keeps increasing, indicating that a memory leak may occur. For web pages that contain a large number of dynamic components and frequently perform DOM operations, the memory curve may fluctuate. Observe the minimum memory value. If the minimum memory value increases gradually, the web page may have a memory leak.

  3. Take the first heap snapshot of the web page. It is found that the Array object occupies 24 MB memory, which is significantly higher than the normal value (usually within several MB). Therefore, a memory leak may occur on this object.

  4. Perform operations that may trigger memory leaks on the page. Take a second heap snapshot after completing the operations, then select "Objects allocated between Snapshot 2 and Snapshot 3". It shows the Array constructor newly occupies approximately 20 MB of memory.

  5. Open the Comparison tab, select snapshot 3, and use snapshot 2 as the reference. The results show the Array constructor created 5,010 new objects, occupying 20.1 MB but releasing only 184 B. Therefore, the memory leak occurred in Array.

  6. Record a heap snapshot for 1 to 2 minutes to obtain a Summary view with a timeline, which is similar to the view on the Performance page. This helps identify which actions caused memory usage changes. Select Allocations on timeline, and click the recording button. After the test action is complete, stop the recording to generate the memory heap timeline view.

  7. On the result timeline, you can drag the left mouse button to select the target time range and view memory allocations during that period. The highlighted area in the image below shows the Array constructor created 2,000 new objects in the selected time frame. This feature clarifies the impact of different operations on memory.