3

I am concerned about memory leaks in my client-side javascript application.

in particular, I have objects which cross-reference each other.

var obj1 = new SomeObject();
var obj2 = new AnotherObject();
obj1.ref = obj2;
obj2.ref = obj1;

as the program runs, obj1 and obj2 are reset

obj1 = new SomeObject();
obj2 = new AotherObject();
obj1.ref = obj2;
obj2.ref = obj1;

the question is whether the original objects which were assigned to obj1 and obj2 will be garbage collected, or do they need to be deleted manually?

cc young
  • 18,939
  • 31
  • 90
  • 148
  • Or to put this another way, will objects with circular references be garbage collected when there are no remaining "external" references? Modern browsers should have no trouble garbage collecting such objects. (Also, you can't `delete` an object per se: you can `delete` an object's property, and if that property was the last reference to some other object then the other object will be garbage collected.) – nnnnnn Jun 28 '13 at 08:47
  • @nnnnnn - your wording much better, thanks. good point on `delete` - what a weird function that is. – cc young Jun 28 '13 at 08:54
  • It's fairly easy to write a loop and test this... – Karoly Horvath Jun 28 '13 at 08:59
  • For more information http://stackoverflow.com/questions/774357/how-does-the-garbage-collection-mechanism-work is also interesting. – Jon Jun 28 '13 at 09:00

0 Answers0