JavaScript 内存释放测试

 

 

JS内部实现垃圾回收机制 无法立即释放 只要数据没有依赖关系 就会在下轮内存回收时清理 

! function ()
  {
    var
      array = []
    , count = 100*100*100*5
    , time = +new Date
    
    ; while( count-- )
          array.push( document.createElement('el') )
    
    ; console.log( +new Date - time, array.length )

    ; count = 100*100*100*5
    ; time = +new Date

    ; while( count-- )
      {
        ; array.pop()          // 无论哪种方法 只要 生成的对象没有依赖 就会被系统释放回收
      //; array[ count ] = null   测试使用null大法 木有用 内存不会立即释放
      //; delete array[ count ]   这种方式在我的浏览器效率超低
      }

    ; console.log( +new Date - time, array.length )

  }()

 

 

! function ()
  {
    var
      array = []
    , count = 100*100*100*5
    , time = +new Date
    
    ; while( count-- )
          array.push( document.createElement('el') )
    
    ; console.log( +new Date - time, array.length )

    ; array = null      //  只要数据没有引用 内存就会被释放 

  }()

posted @ 2014-08-06 23:24  doop  阅读(206)  评论(0编辑  收藏  举报