Netscape 7.1 memory leaks

D

Dennis

It seems that garbage collection is somewhat flawed in Netscape as the
following little script can bring a machine to its knees in about an
hour when run on Netstcape 7.1. I've tried freeing the variables
manually, but Im not sure whats broken. I assume that the images
aren't being freed when they are replaced. Is there a
workaround/solution to this? I've read some old postings about this in
4.x browsers, but I never saw a solution, and its apparently still not
been repaired.

Dennis

<html>
<head>
<script language="JavaScript">
var myimage;

function randgen()
{
var date = new Date();

return date.getTime();
}
function loadIT()
{
myimage=new Image;

myimage.src="someimage.jpg?rand=" + randgen();
setTimeout("loadIT()",1000);
}
loadIT();
</script>
</head>
<body>
</body>
</html>
 
K

kaeli

<script language="JavaScript">
var myimage;

function randgen()
{
var date = new Date();

You keep making a new date. Why not reuse the same one?
Little scripts, this isn't an issue, but if it runs a lot, eventually
you'll run out of memory.
return date.getTime();
}
function loadIT()
{
myimage=new Image;

Same as above.

/old C programmer

--
 
M

Mike

setTimeout("loadIT()",1000);

I beleive this is your problem. You have the loadIT() method call in
quotation marks so basically it is just a meaningless string. To actually
call the method the JavaScript engine will wrap the string in an annonymous
function everytime this line of code is called.

This new annonymous function is added to the window object. Hence the memory
consuption problem, because the life of the annonymous function is equal to
the life of the window object.

The Date object creation is not a problem becuase it's life span is much
shorter, basically just the life of the method call that creates it.

To fix this problem simply rewrite that line of code to be

setTimeout(loadIT,1000);

Now instead of a meanlingless string, you are passing a function handle
which the method simple executes without adding any new objects into memory.

Hope this helps
Mike
 
R

Richard Cornford

Mike said:
I beleive this is your problem. You have the loadIT() method call in
quotation marks so basically it is just a meaningless string. To
actually call the method the JavaScript engine will wrap the string
in an annonymous function everytime this line of code is called.

setTimeout has supported strings as its first argument longer than it
has supported function references as its first argument (and so in more
browsers). It is unlikely that setTimeout would create a function object
in order to execute the string, it would only be necessary to pass it on
to the - eval - function in order to have it executed.
This new annonymous function is added to the window object.

Even if an anonymous function object was created for the execution of a
string argument to setTimeout there is no reason to expect it to be
added to the window object (and if it was added what property name would
be used?).
Hence the
memory consuption problem, because the life of the annonymous
function is equal to the life of the window object.

The continuous creation of Image objects is more likely the source of
the memory leak (assuming that there is one, as no demonstration of the
phenomenon has been provided).
The Date object creation is not a problem becuase it's life span is
much shorter, basically just the life of the method call that creates
it.

To fix this problem simply rewrite that line of code to be

setTimeout(loadIT,1000);

Now instead of a meanlingless string, ...
<snip>

That string was never meaningless, it is valid javascript source code,
as should be provided when a string argument is used with setTimout.

Richard.
 
B

Brian Genisio

Richard said:
setTimeout has supported strings as its first argument longer than it
has supported function references as its first argument (and so in more
browsers). It is unlikely that setTimeout would create a function object
in order to execute the string, it would only be necessary to pass it on
to the - eval - function in order to have it executed.




Even if an anonymous function object was created for the execution of a
string argument to setTimeout there is no reason to expect it to be
added to the window object (and if it was added what property name would
be used?).

This is correct. Spidermonkey (the JS engine in Netscape/Mozilla) will
create function objects as it would an integer or date. All objects are
JSObject types, regardless of being a function, a string, a date, or a
custom type. All objects are subject to garbage collection.
The continuous creation of Image objects is more likely the source of
the memory leak (assuming that there is one, as no demonstration of the
phenomenon has been provided).

It seems like the image objects would be garbage collected as well, but
there could be a cache thing going on. How big is the cache set to? (I
am not sure if this matters or not). Since each Image object is
associated with a cache entry (each image is randomly generated), it
could be rooting the image objects in the Spidermonkey engine.

OP: Does it exhibit this behavior when the cache is lower? How about
when it is turned off?

Can you point us to a link that causes this to happen? Does this happen
in any other browsers? (IE, Safari, Opera, etc)

Just some ideas,
Brian
 
R

Richard Cornford

Brian Genisio wrote:
... . Spidermonkey (the JS engine in Netscape/Mozilla)
will create function objects as it would an integer or date.
All objects are JSObject types, regardless of being a function,
a string, a date, or a custom type. ...
<snip>

If code provided to setTimout as a string argument was executed in an
anonymous function then an - arguments - object would be available to
it. A quick test of:-

setTimeout('alert(typeof arguments);',100);

setTimeout(function(){alert(typeof arguments);},100);

Suggests that the code provided as a string argument is not executed in
an anonymous function on Mozilla browsers.

Richard.
 
B

Brian Genisio

Richard said:
Brian Genisio wrote:


<snip>

If code provided to setTimout as a string argument was executed in an
anonymous function then an - arguments - object would be available to
it. A quick test of:-

setTimeout('alert(typeof arguments);',100);

setTimeout(function(){alert(typeof arguments);},100);

Suggests that the code provided as a string argument is not executed in
an anonymous function on Mozilla browsers.

Richard.

Yeah, sorry, I was not completely clear in what I was saying. I was
saying "If a function object is being created..." in response to your
"Even if an anonymous function object was created for the execution..."
statement. I was making no assumptions about the way setTimeout (or
other similar functions) executes the code, only working off the
prefaced assumption that it does create an anonymous function object.

In reality, I was agreeing completely with what you were saying, by
backing it up with my understanding of the NS JS engine :)

Have a day!
Brian
 
D

Dennis

Richard Cornford said:
Brian Genisio wrote:
<script language="JavaScript">
var myimage;

function randgen()
{
var date = new Date();
You keep making a new date. Why not reuse the same one?
Little scripts, this isn't an issue, but if it runs a lot, eventually
you'll run out of memory.

Well if I dont get a "new date" is ceases to do with the function is
intended to do. Its a local variable, so I would think that garbage
collection would have no problem disposing of local variables.


<snip>

If code provided to setTimout as a string argument was executed in an
anonymous function then an - arguments - object would be available to
it. A quick test of:-

setTimeout('alert(typeof arguments);',100);

setTimeout(function(){alert(typeof arguments);},100);

Given all of this philosophical banter, I've tested it both ways and
it makes no difference in terms of lost memory. In fact I think I've
tested every variation of "normal" code possible, which is why I
originally concluded that the problem had to do with the ".src" object
not being freed when it is replaced. Assuming Im correct, any ideas on
how to explicitly free the image object before replacing it? "new"
should free the old one, I would think. I've also tried:

myImage = new Image;

function()
{
myImage.src=null;
myImage.src="whatever";
}
with the same memory loss. Its quite baffling. I dont know if I
mentioned it, but ALL of these variations show NO memory loss in IE,
no matter how badly I code it (just so someone says something nice
about MS once in a while :)

Dennis
 
B

Brian Genisio

Dennis said:
Well if I dont get a "new date" is ceases to do with the function is
intended to do. Its a local variable, so I would think that garbage
collection would have no problem disposing of local variables.






Given all of this philosophical banter, I've tested it both ways and
it makes no difference in terms of lost memory. In fact I think I've
tested every variation of "normal" code possible, which is why I
originally concluded that the problem had to do with the ".src" object
not being freed when it is replaced. Assuming Im correct, any ideas on
how to explicitly free the image object before replacing it? "new"
should free the old one, I would think. I've also tried:

myImage = new Image;

function()
{
myImage.src=null;
myImage.src="whatever";
}
with the same memory loss. Its quite baffling. I dont know if I
mentioned it, but ALL of these variations show NO memory loss in IE,
no matter how badly I code it (just so someone says something nice
about MS once in a while :)

Dennis

What about the cache thing that I mentioned?

I dont know if I would call what you are experiencing a memory leak. A
memory leak is where memory continues to grow, and the program looses
track of it. I am guessing that Netscape has not lost track of the
memory, but is just holding on to it instead.

You might try asking this question in:
netscape.public.mozilla.dom
netscape.public.mozilla.jseng

Your question might be slightly off topic in both of those groups, but
the people there might have the knowledge to help.

Brian
 
D

Dennis

Brian Genisio said:
What about the cache thing that I mentioned?

I dont know if I would call what you are experiencing a memory leak. A
memory leak is where memory continues to grow, and the program looses
track of it. I am guessing that Netscape has not lost track of the
memory, but is just holding on to it instead.

You might try asking this question in:
netscape.public.mozilla.dom
netscape.public.mozilla.jseng

Your question might be slightly off topic in both of those groups, but
the people there might have the knowledge to help.

Brian


Since it "holds onto it" until the browser is closed (and the OS has
to do it), I think it would qualify as a leak. There's too many bugs
in Mozilla to bother reporting such things, and you'll lose more
customers telling people to upgrade their buggy netscape browsers than
you will telling them to fire up IE in the long run anyway.
 
B

Brian Genisio

Dennis said:
Since it "holds onto it" until the browser is closed (and the OS has
to do it), I think it would qualify as a leak. There's too many bugs
in Mozilla to bother reporting such things, and you'll lose more
customers telling people to upgrade their buggy netscape browsers than
you will telling them to fire up IE in the long run anyway.

Ok, I didnt realize that it held on to the images until the browser was
closed. Are you sure it is not re-using the memory? They may have a
memory manager that takes what it needs, and frees it logically, but not
to the OS. This way, it can use it for other pages. Either way, if it
continues to grow, there is obviously a problem. Too bad no one here
can help. I know I cant think of anything else.

Did you try the netscape groups I mentioned? These are public groups,
with some netscape developers. They my know of work-arounds.

Brian
 
G

Grant Wagner

Dennis said:
It seems that garbage collection is somewhat flawed in Netscape as the
following little script can bring a machine to its knees in about an
hour when run on Netstcape 7.1. I've tried freeing the variables
manually, but Im not sure whats broken. I assume that the images
aren't being freed when they are replaced. Is there a
workaround/solution to this? I've read some old postings about this in
4.x browsers, but I never saw a solution, and its apparently still not
been repaired.

Dennis

<html>
<head>
<script language="JavaScript">
var myimage;

function randgen()
{
var date = new Date();

return date.getTime();
}
function loadIT()
{
myimage=new Image;

myimage.src="someimage.jpg?rand=" + randgen();
setTimeout("loadIT()",1000);
}
loadIT();
</script>
</head>
<body>
</body>
</html>

Garbage collection is a low-priority task. You're creating new objects at
the rate of one a second over an hour. Netscape probably never bothers to
do any garbage collection until the browser is more "idle" then you are
allowing it to be at any point during the execution. As a result it just
continues to request memory from the operating system (which the operating
system is happy to provide up to and beyond the amount of physical memory
present on the system). Once you exceed the amount of physical memory
available, the operating system will begin to swap and you will have
serious performance problems.

Do you really want everyone using that page to be making requests to your
Web server every second? Since you never display the image I'm guessing
you're using it as some sort of "keep alive" session management or
something? HTTP through a Web browser isn't really the proper medium for
something that requires knowing if the client is connected every second.
You're trying to drive a nail with a screwdriver.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
D

Dennis

Grant Wagner said:
Garbage collection is a low-priority task. You're creating new objects at
the rate of one a second over an hour. Netscape probably never bothers to
do any garbage collection until the browser is more "idle" then you are
allowing it to be at any point during the execution. As a result it just
continues to request memory from the operating system (which the operating
system is happy to provide up to and beyond the amount of physical memory
present on the system). Once you exceed the amount of physical memory
available, the operating system will begin to swap and you will have
serious performance problems.

Do you really want everyone using that page to be making requests to your
Web server every second? Since you never display the image I'm guessing
you're using it as some sort of "keep alive" session management or
something? HTTP through a Web browser isn't really the proper medium for
something that requires knowing if the client is connected every second.
You're trying to drive a nail with a screwdriver.

the example provided is designed to accelerate the problem so it
happens in a half hour rather than a day.

Being able to "drive a nail with a screwdriver" has earned me a pretty
good living, and its what sets apart the men from the boys in the
programming field. Bad programmers make excuses; Good programmers find
solutions.

Your management or customers aren't going to care how you got
something done, only that the end result works well, is intuitive and
doesnt cause a 2Ghz machine to run like an IBM XT (ie Java). It helps,
of course, when the tools work as expected.

Dennis
 
G

Grant Wagner

Dennis said:
the example provided is designed to accelerate the problem so it
happens in a half hour rather than a day.

Being able to "drive a nail with a screwdriver" has earned me a pretty
good living, and its what sets apart the men from the boys in the
programming field. Bad programmers make excuses; Good programmers find
solutions.

And shysters use unreliable hacks to achieve a result until their hack comes unglued, then they blame their
tools.
Your management or customers aren't going to care how you got
something done, only that the end result works well, is intuitive and
doesnt cause a 2Ghz machine to run like an IBM XT (ie Java). It helps,
of course, when the tools work as expected.

They also expect it to work long-term and not break with the latest release of the latest "gee whiz" browser.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
D

Dennis

Grant Wagner said:
And shysters use unreliable hacks to achieve a result until their hack comes unglued, then they blame their
tools.


They also expect it to work long-term and not break with the latest release of the latest "gee whiz" browser.

I would say that sabotaging a design that will work on 95% of machines
to accommodate those stupid enough to use "gee-whiz" browsers is the
one whos making the mistake. If you'd rather stand by your square
wheel and pound your chest that it "fits any car" good for you, but my
round wheel is going to blow yours away. Product design is about
making money; about having a better product, not about making
universally mediocre widgets.

You really dont have a point because Im not trying to do anything out
of spec. Nor am I trying to drive a nail with a screwdriver. Netscape
just has a plethora of problems, as they always have. We all know it,
so why are you agruing against it?
 
T

Thomas 'PointedEars' Lahn

Mike wrote:

Who wrote this? said:
setTimeout("loadIT()",1000);

I beleive this is your problem. You have the loadIT() method call in
quotation marks so basically it is just a meaningless string. [...]

This is utter nonsense. All known implementations of setTimeout() accept a
string as first argument, since they have been implemented, to evaluate that
string after the specified timeout (here: 1s). If you would bothered to
read and understand the discussions we had the last days, you would not have
needed to demonstrate your pseudo-knowledge.

Besides, does Charles Newman know that you are abusing his domain by
violating Internet/NetNets standards (RFC 2822: "A mailbox receives
mail.")?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Dennis said:
"Richard Cornford" <[email protected]> wrote in message news:<[email protected]>...

Please do not (let your software) write attribution novels.
Well if I dont get a "new date" is ceases to do with the function is
intended to do. Its a local variable, so I would think that garbage
collection would have no problem disposing of local variables.

One solution may be to assign `null' after the last reference to `date'.
It is a way to "ask" the GC for reference evaluation (i.e. if there is
no reference to the object anymore, it is GC'd).

Since the local `date' is only used in

| return date.getTime();

another solution could be

return new Date().getTime();

And no additional function would be required then, since randgen() is only
used in

| myimage.src="someimage.jpg?rand=" + randgen();

which can be rewritten as

myimage.src = "someimage.jpg?rand=" + new Date().getTime();


BTW: Brian, Dennis, would the two of you please trim your future quotes to
the absolute necessary? See <http://netmeister.org/news/learn2quote.html>.
Thanks in advance.


PointedEars
 
G

Grant Wagner

Dennis said:
I would say that sabotaging a design that will work on 95% of machines
to accommodate those stupid enough to use "gee-whiz" browsers is the
one whos making the mistake. If you'd rather stand by your square
wheel and pound your chest that it "fits any car" good for you, but my
round wheel is going to blow yours away. Product design is about
making money; about having a better product, not about making
universally mediocre widgets.

I'm not talking about sabotaging anything. I'm talking about fixing a design that isn't working in one of the
browsers in that 5% you think is unimportant. But as I've said before, when all you've got is a screwdriver,
everything looks like a nail, so pound away.
You really dont have a point because Im not trying to do anything out
of spec. Nor am I trying to drive a nail with a screwdriver. Netscape
just has a plethora of problems, as they always have. We all know it,
so why are you agruing against it?

Because using client-side code to request an image with random number attached to the end of the URI is leading to
problems, whereas:

<iframe id="session" src="<%= addSessionInfo('session.jsp') %>" width="1" height="1"
style="visibility:hidden;"></iframe>
<ilayer id="sessionNS4" src="<%= addSessionInfo('sessionNS4.jsp') %>" width="1" height="1"
style="display:none;"></ilayer>

-- session.jsp:

<html>
<head>
<meta http-equiv="refresh" content="900; URL=<%= addSessionInfo('session.jsp') %>" />
</head>
<body>
</body>
</html>

-- sessionNS4.jsp:

<html>
<head>
</head>
<body onload="if (document.layers) var t = setTimeout('document.layers[\'sessionNS4\'].src = \'<%=
addSessionInfo('sessionNS4.jsp') %>\'', 900000);">
</body>
</html>

while still relying on client-side technology for the Netscape 4 version, leads to no problems like you describe in
any of: Opera 7.23, IE5.5+, any of the recent Gecko browsers or Netscape 4.7x.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top