1

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

2
  • 1
    Javascript already has a garbage collector that clear any unused references in memory. Commented May 18, 2012 at 7:59
  • I think already the question is asked ,check this link below stackoverflow.com/questions/5115054/… Commented May 18, 2012 at 8:02

3 Answers 3

1

try to set to null.

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

a = null;
alert(a);
Sign up to request clarification or add additional context in comments.

Comments

1

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;

1 Comment

I understand these things very well. But I would like to free the memory explicitly. Since, my objects are globally used and the memory has to be cleared when these objects are no needed further.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.