object1作为object2的属性,而object2又是object1的属性 - 会导致内存泄漏吗? [英] object1 as a property of object2 which is in turn a property of object1 - will result in memory leak?
问题描述
form和form.errorProcessor只是保持对彼此的单个引用,并且在内存中只有2个对象,或者这是泄漏/问题情况?
Will "form" and "form.errorProcessor" just hold single references to each other and have only 2 objects in memory, or is this a leak / problem situation?
var ErrorProcessor = function(form){
this.form = form; // Problem Line
}
var form = $("form");
form.errorProcessor = new ErrorProcessor(form); // Some element assignment
发布问题行后,我有一个对象层次结构,如下所示[由控制台检查] .log(form)]
Post the problem line, I have an object hierarchy as below [checked by console.log(form)]
form:{errorProcessor:{form:{errorProcessor:{form:{errorProcessor:{...}}}}}}
form: {errorProcessor: {form: {errorProcessor: {form: {errorProcessor: {...}}}}}}
推荐答案
不,循环引用没有内存泄漏,并且您在代码中只创建了两个对象。任何体面的垃圾收集器都可以处理它们。
No, there is no memory leak caused by circular references, and you are creating only two objects in your code. Any decent garbage collector can handle them.
唯一的问题是当您递归检查对象时(就像在控制台中扩展其属性一样),无底洞 - 如果以编程方式完成,您将从递归中获得无限循环或堆栈溢出。但是,您在控制台中看到的所有这些结构都代表同一个对象。
The only problem is when you recursively inspect your object (like you do with expanding its properties in the console), you go down a bottomless pit - if done programmatically, you'd get an infinite loop or stack overflow from recursion. All these structures you see in your console are representing the same object, though.
这篇关于object1作为object2的属性,而object2又是object1的属性 - 会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!