javascript simple object creation test: opera leaks?
<p>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:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
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&lt;400000; i++) {
    a[i] = new Obj("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  }

  d.innerHTML = "PAGE LOADED";
}

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

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

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>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.</p>

<p>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?</p>

<p>Thanks!
-joe</p>

<p><hr/>
<b>Update:</b><br/>
I accepted chris' answer. The support link didn't directly help, but it got me thinking (always a good thing).</p>

<p>Per the support docs I tried setting opera.setOverrideHistoryNavigationMode='compatible' in my JS, but it didn't change anything. As I understand the docs, this method takes priority over any opera:config setting, etc.</p>

<p>But, I tend to agree that there's some Opera optimization going on here. I'm not an Opera user, so am just getting familiar with all their config stuff for this test...</p>

<p>I turned off all memory caching in Opera user prefs for this test. This worked. Memory was reclaimed on closing the window. So it cannot be a leak.</p>

<p>One other thing I noticed:
In Firefox and Safari, and very possibly other browsers, if there's no window.onunload handler then memory is only reclaimed when the window is closed. Memory is NOT reclaimed if the user navigates elsewhere. So if you load a huge page full of JS objects, then spend the rest of the day browsing plain HTML pages, you don't get the memory back for a long time.</p>

<p>But, if you include a simple "do nothing" window.onunload handler, like:
    window.onunload=dummy;
    function dummy() {}
then memory is reclaimed even on navigation away from the page. From what little I've been able to read on the subject, this seems to be known, desired behavior.</p>

<p>Opera is different. Even with memory cache disabled, memory is only reclaimed on window close, not on unload-by-navigation. Go figure.</p>
