不卡AV在线|网页在线观看无码高清|亚洲国产亚洲国产|国产伦精品一区二区三区免费视频

學習啦 > 學習電腦 > 電腦硬件知識 > 內(nèi)存知識 > IE6內(nèi)存泄漏怎么解決

IE6內(nèi)存泄漏怎么解決

時間: 孫勝龍652 分享

IE6內(nèi)存泄漏怎么解決

  大家都知道,內(nèi)存是電腦必不可少的一個硬件,所以說,關于內(nèi)存會有各種各樣的問題,學習啦小編就在這里給大家介紹IE6內(nèi)存泄漏怎么解決。

  Hedger Wang 在國內(nèi) blog 上得到的方法:使用 try … finally 結(jié)構(gòu)來使對象最終為 null ,以阻止內(nèi)存泄露。

  其中舉了個例子:

  function createButton() {

  var obj = document.createElement("button");

  obj.innerHTML = "click me";

  obj.onclick = function() {

  //handle onclick

  }

  obj.onmouseover = function() {

  //handle onmouseover

  }

  return obj;//return a object which has memory leak problem in IE6

  }

  var dButton = document.getElementById("d1").appendChild(createButton());

  //skipped....

  對于 IE6 中,引起內(nèi)存泄露的原因,可看《Understanding and Solving Internet Explorer Leak Patterns》一文。

  上面的例子,應該屬于上文中的 “Closures”原因。

  再看下用 try … finally 的解決方法:

  /**

  * Use the try ... finally statement to resolve the memory leak issue

  */

  function createButton() {

  var obj = document.createElement("button");

  obj.innerHTML = "click me";

  obj.onclick = function() {

  //handle onclick

  }

  obj.onmouseover = function() {

  //handle onmouseover

  }

  //this helps to fix the memory leak issue

  try {

  return obj;

  } finally {

  obj = null;

  }

  }

  var dButton = document.getElementById("d1").appendChild(createButton());

  //skipped....

  可能大家有疑問: finally 是如何解析的呢?

  答案是:先執(zhí)行 try 語句再執(zhí)行 finally 語句。

  例如:

  function foo() {

  var x = 0;

  try {

  return print("call return " ( x));

  } finally {

  print("call finally " ( x));

  }

  }

  print('before');

  print(foo());

  print('after');

  返回的結(jié)果為:

  print » before

  print » call return 1

  print » call finally 2

  print » true

  print » after

297648