简体   繁体   English

JavaScript内存管理(信息)

[英]JavaScript Memory Management (Info)

What I am trying to work out is whether nullifying is always necessary. 我试图解决的是是否总是需要取消操作。 As I am having a few issues with Chrome. 由于我在使用Chrome时遇到了一些问题。 Say I have this code. 说我有这段代码。

function MyClass() {}

MyClass.prototype.MyFunction = function() {
    this.data = "foo";
}
var c = new MyClass();
c.MyFunction();

Now once that function is called it should be allowed to be GC but should the end of the function have this.data = null . 现在,一旦调用了该函数,就应该允许它为GC,但是该函数的末尾应该具有this.data = null Should this also be standard. 这也应该是标准的。

Your code will not work. 您的代码将无法正常工作。 You should first create an instance of your class: 您应该首先创建您的类的实例:

var c = new MyClass();
c.MyFunction();

because MyFunction is instance function. 因为MyFunction是实例函数。

Otherwise there's also delete operator ( reference and an indepth analysis ), that is used to remove object members (but not objects themselves). 否则,还有delete操作符( 参考深入分析 ),用于删除对象成员(但不删除对象本身)。 Objects can therefore be garbage collected when there's no way to reference them any more, so 因此,当无法再引用对象时,就可以对其进行垃圾回收。

c = undefined;

should convince Javascript to garbage collect this object instance and release memory resources taken by it. 应该说服Javascript垃圾收集此对象实例并释放它占用的内存资源。

It is different if your object instance is instantiated this way: 如果用这种方式实例化对象实例,则有所不同:

c = new MyClass();
c.MyFunction();
delete c; // success

because c is this time a member of global ( window ) and can therefore be deleted from it. 因为c这次是global( window )的成员,因此可以从中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM