This is definitely a memory leak. However, memory consumption is so small and cannot be measured. I made minor changes to the source code.
- I put all the code inside the loop to create the same script 100,000 times
- I increased the timer interval to about 16 minutes. This prevents browser crashes.
Here is the code:
for (var i = 0; i < 100000; i++) { var buggyObject = { callAgain: function() { var ref = this; var val = setTimeout(function() { ref.callAgain(); }, 1000000);

My experiment:
I ran the code in Chrome version 34.0.1847.116 m and committed the memory changes using the \ Timeline developer tools.
As can be seen from the figure, about 32 MB of memory was used to run this code, and after a while it decreased to about 30 MB and remained unchanged (see No. 1). After several attempts to collect garbage using Chrome (see No. 2) and one manual garbage collection (see No. 3, No. 4), memory consumption remains unchanged. There is no longer buggyObject , and we can do nothing to free up memory. The only possible way is to close the browser.
What causes this?
The main reason for this behavior is the timer. A timer callback and its associated buggyObject will not be returned until a timeout occurs. In our case, the timer resets itself and starts forever, and therefore its memory space will never be collected, even if there is no reference to the original object.
source share