Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do javascript memory leaks matter after a page refresh? Why?

Tags:

javascript

I've often wondered this. After a page refresh doesn't the browser throw everything away?

Thanks

like image 886
tony Avatar asked Mar 06 '15 09:03

tony


2 Answers

It depends.

Some browsers, like Opera 12.17 and older, run in a single process.
These have a limited memory access, and do a bunch of stuff to the same address space.

Others, like Google Chrome, have a process per page and the process may be deleted after a refresh.

But, since it leaked, it may be outside the boundaries that the browser currently is controlling (e.g.: may be put in the swap).

There, there is no guarantee that it will be deleted.

Also notice that browsers may have bugs on the Garbage Collector and leave a few objects behind (like old IE versions).
This won't go away with a refresh.
Consider this very basic code:

var all = document.getElementsByTagName('*');

for(var i=all.length-1; i; i--)
{
    all[i].parentNode.removeChild(all[i]);
}

document.write('<p>New content</p>');

all = document.getElementsByTagName('*');

On Google Chrome, open the 'Task Manager' (Menu > More Tools > Task Manager).

Open a new tab and go to http://google.com and run the above code.
Keep an eye on the memory usage.
It should be around 25000kB.

Run this code.
You will notice that the memory will go up to around 40000kB.
Refresh and repeat.
Just watch how the memory usage goes up...

I would love to say "Yes", but browsers are very complex programs.

like image 129
Ismael Miguel Avatar answered Sep 28 '22 02:09

Ismael Miguel


After a page refresh doesn't the browser throw everything away?

It should do, yes. So if your page has a memory leak, over time it will consume more and more memory until/unless the user refreshes it. (I have to refresh my Gmail tab every few days for exactly this reason.)

like image 42
T.J. Crowder Avatar answered Sep 28 '22 01:09

T.J. Crowder