Pacerier
Pacerier

Reputation: 89693

Does circular references cause memory leaks in JavaScript?

I've read many claims stating that circular references will cause memory leaks:

So I tested this script on Chrome / FF4 / IE9 / Opera / Safari:

<script>
var a, b, list = [];

for(var x=0;x<1000000;x+=2){
    a = list[x] = document.createElement("div");
    b = list[x + 1] = document.createElement("div");
    a.hook = b;
    b.hook = a;
}
</script>

Task manager shows that the script consumes ~0.2 GB of memory.

Then I refreshed the page 10 times. If the script does cause memory leaks, after 10 refreshes I should be seeing ~2 GB of memory consumed, yet task manager shows otherwise.

In which browsers do circular references cause memory leaks in JavaScript?

Upvotes: 2

Views: 412

Answers (1)

alex
alex

Reputation: 490403

Memory leaks were mainly an issue in older IEs with circular references and reference counting garbage collection that never quite reclaimed everything (never a reference count of 0 in circular references).

I think memory leaks in IE have been fixed since IE8.

Microsoft have a in depth article on the subject.

Upvotes: 4

Related Questions