最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Using JavaScript with Internet Explorer, how do I clear memory without refreshing the page? - Stack Overflow

programmeradmin6浏览0评论

I have an AJAX-based website using JavaScript on the client. Certain operations on the site cache large result sets from service calls in the browser (i.e. hundreds of megabytes). These are throw-away results. They will be viewed for a short time and then need to be cleared from memory.

I've written a small test site that loads a bunch of junk in memory and then calls JavaScript's delete method. This works great in Firefox (memory almost instantly gets returned). Internet Explorer 8 (haven't tried 7) doesn't free the memory until the page is refreshed or closed.

Does anyone know how to drop IE's memory usage using JavaScript/Ajax (no page refreshes)?

Below is my sample client code:

function load() {
 var x = ['dfjasdlfkjsa;dflkjsad;flkjsadf;lj'];

 for( var i = 0; i < 10000000; ++i ) {
  x.push('asdfasfasfsfasdfkasjfslafkjslfjsalfjsaldfkjasl;dfkjsadfl;kjsdflskajflskfjslakfjaslfkjsaldfkjsaldfksdfjk');
 }

 alert('deleting');   // <--- memory usage around 500mb
 delete x;            // <--- immediate results in Firefox 3.5 (not IE8)
 alert('done');
}

UPDATE: Setting the variable to 'null' does not immediately clear the memory (as that is left up to the garbage collector). Also, setting a variable null only gets a single reference where there might be multiple references.

I have an AJAX-based website using JavaScript on the client. Certain operations on the site cache large result sets from service calls in the browser (i.e. hundreds of megabytes). These are throw-away results. They will be viewed for a short time and then need to be cleared from memory.

I've written a small test site that loads a bunch of junk in memory and then calls JavaScript's delete method. This works great in Firefox (memory almost instantly gets returned). Internet Explorer 8 (haven't tried 7) doesn't free the memory until the page is refreshed or closed.

Does anyone know how to drop IE's memory usage using JavaScript/Ajax (no page refreshes)?

Below is my sample client code:

function load() {
 var x = ['dfjasdlfkjsa;dflkjsad;flkjsadf;lj'];

 for( var i = 0; i < 10000000; ++i ) {
  x.push('asdfasfasfsfasdfkasjfslafkjslfjsalfjsaldfkjasl;dfkjsadfl;kjsdflskajflskfjslakfjaslfkjsaldfkjsaldfksdfjk');
 }

 alert('deleting');   // <--- memory usage around 500mb
 delete x;            // <--- immediate results in Firefox 3.5 (not IE8)
 alert('done');
}

UPDATE: Setting the variable to 'null' does not immediately clear the memory (as that is left up to the garbage collector). Also, setting a variable null only gets a single reference where there might be multiple references.

Share Improve this question edited Sep 1, 2009 at 16:00 Jordan Parmer asked Sep 1, 2009 at 15:48 Jordan ParmerJordan Parmer 37.3k30 gold badges99 silver badges120 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

IE (well, technically, JScript) has an undocumented CollectGarbage method, which supposedly forces garbage collector to run immediately. You might want to play with that, but from my experience, nulling references is enough most of the time.

Instead of using the delete method, set the variable to null. This seems to be the best way to clear up memory cross-browser (delete is notoriously flaky). It works the same as my other answer here.

alert('deleting');   // <--- memory usage around 500mb
x = null;
alert('done');

And if you try "x= null;" instead of delete ?

Just assign null to the variable:

x = null;

I realize you mentioned the data is throw away, but, when you delete the nodes, even if just setting x to null, you need to make certain that no event handlers are attached to any of the nodes, otherwise that node cannot be garbage collected.

Here is a function I use in my own code: http://javascript.crockford./memory/leak.html

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论