English 中文(简体)
Clear object memory in javascript
原标题:

I am using General Interface in my web application and I have javascript classes and methods to create objects for my classes. I would like to clear the memory when the objects are not used. My question is how can I clear the object s memory.

I have tried with obj = null; and delete obj; . Both are not working as expected.

Is there way to clear the object and object memory in JavaScript or in General Interface.

-Sridhar

最佳回答

You can t. As long as every reference is really removed (e.g. setting to null as many have suggested), it s entirely up to the GC as to when it ll run and when it ll collect them.

问题回答

try to set to null.

var a = new className();
alert(a);

a = null;
alert(a);

You can use Self-Invoking Functions

Self-invoking functions are functions who execute immediately, and create their own closure. Take a look at this:

(function () {
    var dog = "German Shepherd";
    alert(dog);
})();
alert(dog); // Returns undefined

so the dog variable was only available within that context

EDIT
If memory leak is related to DOM, here written how to manage it. So, i tried to solve like that:

var obj = {};//your big js object
//do something with it

function clear() {
    var that = this;
    for (var i in that) {
        clear.call(that[i]);
        that[i] = null;
    }
}

clear.call(obj);//clear it s all properties
obj = null;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签