开发者

javascript simple object creation test: opera leaks?

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


Update:

I accepted chris' answer. The support link didn't directly help, but it got me thinking (always a good thing).

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.

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

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.

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.

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.

Opera is different. Eve开发者_StackOverflown with memory cache disabled, memory is only reclaimed on window close, not on unload-by-navigation. Go figure.


I really don't know, but maybe it's related to this. http://www.opera.com/support/kb/view/827/

Opera's crazy fast back button must be doing something. I noticed it feels a bit different since they released 10.5(some pages feel slower...maybe they tuned it), so maybe that's why some people here say they can't reproduce the memory leak.


It works fine on Opera 10.5 on Windows. No leaks here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜