---
title: "用Chrome-Profiles检查Javascript内存泄露问题"
author: "yutiansky"
date: "2012-03-07T19:07:20Z"
tags: []
categories: ["Web前端"]
slug: "chrome-profiles-javascript"
draft: false
lastmod: "2012-03-07T19:08:49Z"
id: 1443378
url: "https://dncsoft.iteye.com/blog/1443378"
---

贴几个图说明如果用Chrome-Porfiles检查JavaScript的内存泄漏问题。

 

一，先写个JavaScript的例子。

```
var myArray = new Array();
var count = 0;
function leakMemory() {
    var arr = new Array();
    arr[0] = new Array(10000).join("Y");
    
    // leak memory here
    myArray[count] = new Array(10000).join("X");
    count++;
}
function freeMemory() {
    // free memory here
    myArray = null;
    myArray = new Array();
    count = 0;
}
```

 

 

二，下图就是Chrome-Profiles的画面。

![](http://dl.iteye.com/upload/attachment/0064/4147/62a78a4c-74c8-36a4-894c-ead45fe5156a.png)

 

 

三，先取得1个【SNAPSHOTS】

![](http://dl.iteye.com/upload/attachment/0064/4149/cf79beb8-7972-3215-a486-1624c5080074.png)

 

 

四，运行分配内存的模块后，再取得【SNAPSHOTS】，与前次取得的【SNAPSHOTS】进行比较。

![](http://dl.iteye.com/upload/attachment/0064/4151/f35492cc-693e-3961-bc7f-1675906092b3.png)

 

能看到有内存增加。

 

 

//END