javascript simple object creation test: opera leaks?

Posted by joe on Stack Overflow See other posts from Stack Overflow or by joe
Published on 2010-04-02T21:00:27Z Indexed on 2010/04/02 21:03 UTC
Read the original article Hit count: 273

Filed under:

Hi,

I am trying to figure out certain memory leak conditions in javascript on a few browsers. Currently I'm only testing FF 3.6, Opera 10.10, and Safari 4.0.3. I've started with a fairly simple test, and can confirm no memory leaks in Firefox and Safari. But Opera just takes memory and never gives it back. What gives? Here's the test:

<html>
<head>
<script type="text/javascript">
window.onload = init;
//window.onunload = cleanup;
var a=[];
function init() {
  var d = document.createElement('div');
  d.innerHTML = "page loading...";
  document.body.appendChild(d);

  for (var i=0; i<400000; i++) {
    a[i] = new Obj("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  }

  d.innerHTML = "PAGE LOADED";
}

function cleanup() {
  for (var i=0; i<400000; i++) {
    a[i] = null;
  }
}

function Obj(msg) {
  this.msg=msg;
}

</script>
</head>
<body>
</body>
</html>

I shouldn't need the cleanup() call on window.unload, but tried that also. No luck. As you can see this is simple JS, no circular DOM links, no closures. I monitor the memory usage using 'top' on Mac 10.4.11. Memory usage spikes up on page load, as expected. In FF and Safari reloading the page does not use any further memory, and all memory is returned when the window (tab) is closed. In Opera, memory spikes on load, and seems to also spike further on each reload (but not always...). But regardless of reload, memory never goes back down below the initial load spike.

I had hoped this was a no-brainer test that all browsers would pass, so I could move on to more "interesting" conditions. Am I doing something wrong here? Or is this a known Opera issue?

Thanks! -joe

© Stack Overflow or respective owner

Related posts about JavaScript