我在我的Web應用程序中使用常規接口,並且我有用於爲我的類創建對象的JavaScript類和方法。我想在不使用對象時清除內存。 我的問題是如何清除對象的內存。在javascript中清除對象內存
我試過'obj = null;'和'delete obj'。兩者都沒有按預期工作。
有沒有辦法在JavaScript或通用接口中清除對象和對象內存。
-Sridhar
我在我的Web應用程序中使用常規接口,並且我有用於爲我的類創建對象的JavaScript類和方法。我想在不使用對象時清除內存。 我的問題是如何清除對象的內存。在javascript中清除對象內存
我試過'obj = null;'和'delete obj'。兩者都沒有按預期工作。
有沒有辦法在JavaScript或通用接口中清除對象和對象內存。
-Sridhar
你不能。只要每個參考文件都被真正刪除(例如設置爲null
,正如許多人所建議的那樣),完全取決於GC何時會運行以及何時會收集它們。
試着設置爲null
。
var a = new className();
alert(a);
a = null;
alert(a);
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,here書面如何管理它。所以,我試圖這樣解決:
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;
我很瞭解這些事情。但我想明確地釋放內存。因爲,我的對象是全局使用的,並且當這些對象不再需要時,內存必須被清除。 –
JavaScript已經有一個垃圾收集器,清除內存中任何未使用的引用。 – fcalderan
我認爲已經問過這個問題,請查看下面的鏈接 http://stackoverflow.com/questions/5115054/delete-object-from-memory-in-javascript – aravindKrishna