从内存中删除预加载的图像 [英] Deleting a preloaded image from memory

查看:76
本文介绍了从内存中删除预加载的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从内存/磁盘缓存中真正删除预加载的图像?让''s

说我通过创建一个Image对象并将其src

属性设置为所需的URL来预加载图像:


var img = new Image();

img.src = [someurl];


然后我将图像添加到图像中几次一个数组

对象:

var images = new Array();

images.push(img);


我发现只是在指向那个图像

对象的指针上调用''删除'',并没有释放RAM。


删除图像[0]; //这里没有释放RAM!


我应该在对图像对象的所有引用上调用delete吗?这个

将是一个真正的痛苦,因为我必须检查大约3000行代码

可能引用它并确保我'删除''所有这些...

其他新的Image对象,它们的src属性

设置为相同的URL怎么样?

How can I really delete a preloaded image from memory/disk cache? Let''s
say I preload an image by creating an Image object and setting its src
attribute to desired URL:

var img = new Image();
img.src = [someurl];

Then I use the image a few more times by adding it into an Array
object:

var images = new Array();
images.push(img);

I found out that just calling ''delete'' on a pointer to that Image
object, doesn''t free up the RAM.

delete images[0]; //RAM is not freed here!

Should I call delete on all the references to the image object? This
would be a real pain, since I have to check about 3000 lines of code
for possible references to it and make sure I ''delete'' all of them...
What about the other new Image objects, that get their src attribute
set to the same url?

推荐答案

iv ******** @ gmail.com 写道:
iv********@gmail.com wrote:
如何真正从内存/磁盘缓存中删除预加载的图像?让我们说我通过创建一个Image对象并将其src
属性设置为所需的URL来预加载图像:

var img = new Image();
img.src = [someurl];


img = null;

然后我通过将图像添加到数组
对象中多次使用该图像:

var images = new Array();
images.push(img);

我发现只是在指向该Image的指针上调用''delete'' >对象,没有释放RAM。

删除图像[0]; //这里没有释放RAM!


images = null;

[...]
How can I really delete a preloaded image from memory/disk cache? Let''s
say I preload an image by creating an Image object and setting its src
attribute to desired URL:

var img = new Image();
img.src = [someurl];
img = null;
Then I use the image a few more times by adding it into an Array
object:

var images = new Array();
images.push(img);

I found out that just calling ''delete'' on a pointer to that Image
object, doesn''t free up the RAM.

delete images[0]; //RAM is not freed here!
images = null;
[...]




希望这有帮助,


-

Bart



Hope this helps,

--
Bart


设置一个指向null的指针显然赢了'' t从内存中删除指向

的对象,但只需更改指针引用的地址

(从对象的地址到null = 0x0000000)。如果管理内存并且垃圾收集器发现

没有更多对相关对象的引用,则该对象可能会被删除

。但是如果其他指针仍然引用同一个对象,那么内存管理器

会将它保留在内存中。这就是它的工作原理。


你给我的解决方案不仅没用,而且效果甚至比我写的那个更有效。问自己。还有其他想法吗?

Setting a pointer to null obviously won''t delete the object pointed to
from memory, but just change the address the pointer is referencing
(from address of the object to null = 0x0000000). The object might get
deleted if the memory is managed and a garbage collector finds out
there is no more reference to the object in question. However if any
other pointer is still referencing the same object, the memory manager
will keep it in memory. That''s just how it works.

The solution you gave me is not just useless, but even less effective
than the one I wrote in the question myself. Any other ideas?


iv **** **** @gmail.com 写道:
iv********@gmail.com wrote:
设置指向null的指针显然不会从内存中删除指向
的对象,而只是更改指针引用的地址
(从对象的地址到null = 0x0000000)。如果管理内存并且垃圾收集器发现没有更多对相关对象的引用,则可能会删除该对象。但是,如果任何
其他指针仍然引用相同的对象,内存管理器将把它保存在内存中。
Setting a pointer to null obviously won''t delete the object pointed to
from memory, but just change the address the pointer is referencing
(from address of the object to null = 0x0000000). The object might get
deleted if the memory is managed and a garbage collector finds out
there is no more reference to the object in question. However if any
other pointer is still referencing the same object, the memory manager
will keep it in memory.




我已经完成了关于这个问题的一些调查。我的方式(设置为

''null'')和你的方式(删除方法)似乎没有释放任何内存

的对象(MSIE上的CPU基准测试)和FF)。我的结论是,

没有可靠的方法来做你想要的。只有一个JScript

功能:


CollectGarbage();


但是这是(明确的) )无证件和(明确)非官方的

虽然,这通常有充分的理由。


一些很好的参考和引用:
http://groups.google .com / group / comp .... e35c3a102831 /:


"问题是你真的没办法知道什么时候

垃圾收集器将释放内存。这是一项优先级低的任务

http://msdn.microsoft.com/msdnmag/is.../default.aspx:


现在,你可以强制JScript垃圾收集器使用

CollectGarbage()方法运行,但我不推荐它。具有GC的JScript的全部要点是,您不必担心对象

生命周期。如果您确实担心它,那么您可能会使用错误的

工具来完成工作!

http://msdn.microsoft.com/msdnmag/is.../default.aspx:


JScript旨在提供一种快速,简便的方法,在网页上编写简单的
脚本。你问的事实让我觉得你在使用错误的工具来做这件事。

http://groups.google.com/group/微... 811edbedf33d /:


"是的,调用CollectGarbage()是错误的。这是因为

的原因没有记录。它不会解决你的任何问题。


-

巴特



I''ve done some investigation about this issue. My way (setting to
''null'') and your way (delete method) do not seem to free up any memory
of the objects (CPU benchmarks on MSIE and FF). My conclusion is that
there is no reliable way to do what you want. There''s only one JScript
function:

CollectGarbage();

But that is (explicitly) undocumented and (explicitly) unofficial
though, and that usually has a good reason.

Some good references and quotes:

http://groups.google.com/group/comp....e35c3a102831/:

"The problem is that you don''t really have a way to know when the
garbage collector will free the memory. It is a task with low priority"

http://msdn.microsoft.com/msdnmag/is.../default.aspx:

"Now, you can force the JScript garbage collector to run with the
CollectGarbage() method, but I don''t recommend it. The whole point of
JScript having a GC is that you don''t need to worry about object
lifetime. If you do worry about it then you''re probably using the wrong
tool for the job!"

http://msdn.microsoft.com/msdnmag/is.../default.aspx:

"JScript was designed to provide a quick, easy way to write simple
scripts on Web pages. The fact that you''re asking at all makes me think
that you''re using the wrong tool for the job."

http://groups.google.com/group/micro...811edbedf33d/:

"Yes, calling CollectGarbage() is wrong. It''s undocumented for a
reason. It won''t solve any of your problems."

--
Bart


这篇关于从内存中删除预加载的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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