Netscape 7.1内存泄漏 [英] Netscape 7.1 memory leaks

查看:60
本文介绍了Netscape 7.1内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎垃圾收集在Netscape中有些瑕疵,因为在Netstcape 7.1上运行时,小脚本后面的

可以让机器in $ $ $
小时。我已经尝试手动释放变量

,但我不知道什么是破碎的。我认为图像

在被替换时不会被释放。是否有一个

解决方法/解决方案?我已经在

4.x浏览器中阅读了一些关于此问题的旧帖子,但我从未见过解决方案,而且它显然仍然没有被修复过。


丹尼斯


< html>

< head>

<脚本语言=" JavaScript">

var myimage;


函数randgen()

{

var date = new Date();


return date.getTime();

}

函数loadIT()< br $>
{

myimage = new Image;


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

setTimeout(" loadIT()",1000);

}

loadIT();

< / script>

< / head>

< body>

< / body>

< / html>

解决方案

文章< 3b ************* ************@posting.google.com> ,
de****@etinc.com 启发我们......

< script language =" JavaScript">
var myimage;

函数randgen()
{
var date = new Date() ;


你一直在约会。为什么不重复使用同一个?

小脚本,这不是问题,但是如果它运行很多,最终会因为内存不足而导致内存耗尽。

返回date.getTime();
}
函数loadIT()
{
myimage = new Image;



与上述相同。


/老C程序员


-

-

~kaeli~

如果Hokey Pokey是它的全部内容怎么办?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


< BLOCKQUOTE>> setTimeout(" loadIT()",1000);


我相信这是你的问题。你在

引号中调用了loadIT()方法,所以基本上它只是一个无意义的字符串。实际上

调用方法,每次调用这行代码时,JavaScript引擎都会将字符串包装在一个匿名的

函数中。


这个新的匿名函数被添加到window对象中。因此记忆

的消耗问题,因为匿名函数的寿命等于窗口对象的生命周期。


Date对象的创建不是问题,因为它的生命周期更短了,基本上只是创建它的方法调用的生命周期。


要解决这个问题,只需将该行代码重写为


setTimeout(loadIT,1000);


现在而不是无意义字符串,你传递一个函数句柄

这个方法简单执行而不会在内存中添加任何新对象。


希望这有助于

Mike


Mike写道:

setTimeout(" loadIT()",1000);
我相信这是你的问题。你在
引号中调用了loadIT()方法,所以基本上它只是一个无意义的字符串。为了实际调用该方法,每次调用这行代码时,JavaScript引擎都会将字符串
包装在一个匿名函数中。




setTimeout has支持的字符串作为它的第一个参数比它更长

支持函数引用作为它的第一个参数(因此在更多

浏览器中)。 setTimeout不太可能为了执行字符串而创建一个函数对象

,只需将它按

传递给 - eval - 函数按顺序让它执行。

这个新的匿名函数被添加到窗口对象中。


即使为执行setTimeout的

字符串参数创建了一个匿名函数对象,也没有理由期望它是

添加到窗口对象(如果添加了什么属性名称将使用
?)。

因此内存消耗问题,因为匿名
函数的生命等于窗口对象的生命。


连续创建Image对象更可能是内存泄漏的来源(假设有一个,因为没有演示
现象已经提供)。

Date对象创建不是问题,因为它的寿命要短得多,基本上只是方法调用的生命周期创建
它。

要解决这个问题,只需将该行代码重写为

setTimeout(loadIT,1000);

>现在而不是一个无意义的字符串,...



< snip>


该字符串从来没有意义,它是有效的javascript源代码代码,

当字符串参数与setTimout一起使用时应提供。


Richard。


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>

解决方案

In article <3b*************************@posting.google.com> ,
de****@etinc.com enlightened us with...

<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

--
--
~kaeli~
What if the Hokey Pokey IS what''s it''s all about?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


> 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


Mike wrote:

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.



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.


这篇关于Netscape 7.1内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆