1

我在我的 Web 应用程序中使用通用接口,并且我有 javascript 类和方法来为我的类创建对象。我想在不使用对象时清除内存。我的问题是如何清除对象的内存。

我试过'obj = null;' 和'删除 obj;'。两者都没有按预期工作。

有没有办法在 JavaScript 或通用接口中清除对象和对象内存。

-斯里达尔

4

3 回答 3

1

尝试设置为null.

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

a = null;
alert(a);
于 2012-05-18T08:03:38.887 回答
1

您可以使用自调用函数

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

编辑
如果内存泄漏与 DOM 有关,这里写了如何管理它。所以,我试图这样解决:

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;
于 2012-05-18T08:17:48.880 回答
0

你不能。只要确实删除了每个引用(例如,设置null为建议的数量),就完全取决于 GC 何时运行以及何时收集它们。

于 2012-05-21T10:26:37.237 回答