Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Deleting a preloaded image from memory

Reply
Thread Tools

Deleting a preloaded image from memory

 
 
ivan.leben@gmail.com
Guest
Posts: n/a
 
      05-17-2006
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?

 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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

> [...]


Hope this helps,

--
Bart

 
Reply With Quote
 
 
 
 
ivan.leben@gmail.com
Guest
Posts: n/a
 
      05-17-2006
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?

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-17-2006
(E-Mail Removed) wrote:

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


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

 
Reply With Quote
 
ivan.leben@gmail.com
Guest
Posts: n/a
 
      05-17-2006
Yes, garbage collector is a low-priority task that should run
automatically in JavaScript, just as it does in Java, C# and other
'modern' languages. Its designed to 'ease' the memory handling. But the
more I think about my problem, the more I get the sense that it's
probably not really connected to garbage collector.

I think objects that get cached by a browser are handled on
another/higher level, than other "programmatical" objects, created by a
script. In fact we are speaking about two different kinds of memory
data - one is the data that is put in cache (image bytes, html text)
and the other is data of DOM programming objects (which make a language
'object oriented').

The programming objects are handled by a 'garbage collector' and are
freed after no pointer references them anymore. On the other side, the
cached data can still be left there (in cache) even after one closes
the browser application (that means in the 'disk cache' memory) in
order to make the rendering of the page faster next time in a short
period. The pure proof of this is that even when i close the browser
(and I see that RAM gets freed), the next time I open it and run the
same web application preloading the same images, they get 'loaded' in
an instance.

There should be (if they exist at all) some other functions not related
to garbage collector to manage the data that is cached. I know how to
explicitly make a page not being cached, by adding some special tags
into the html file. However this is not my preference, because I want
to be able to preload images when I need to, so I wonder: is there any
way to control (delete) the cached data once its cached?

 
Reply With Quote
 
ivan.leben@gmail.com
Guest
Posts: n/a
 
      05-17-2006
To clarify my problem I will give an example of what I'm really trying
to achieve:

My application preloads images one after another when the user browses
a gallery, so there are always 10 images loaded in advance. These
images get shown immediately when the user reaches them, so she doesn't
have to wait.

Now when the user had already browsed over 100 of images, I would like
to delete some of them at the beginning of the 'playlist' and free the
memory so my application can run this way for hours.

How do I achieve this?

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-18-2006
(E-Mail Removed) wrote:

> To clarify my problem I will give an example of what I'm really trying
> to achieve:
>
> My application preloads images one after another when the user browses
> a gallery, so there are always 10 images loaded in advance. These
> images get shown immediately when the user reaches them, so she doesn't
> have to wait.


I think you're looking for a functionality like SQL's FLUSH or Perl's
buffer disable/clean on command. I don't believe this can be controlled
on-the-fly in browsers, though there are META-instructions to control
this on page-level:

<META HTTP-EQUIV="cache-control" CONTENT="no-cache">
<META HTTP-EQUIV="expires" CONTENT="Thu, 8 May 2006 11:30:00 GMT">
(or whatever date)
<META HTTP-EQUIV="pragma" CONTENT="no-cache">

> Now when the user had already browsed over 100 of images, I would like
> to delete some of them at the beginning of the 'playlist' and free the
> memory so my application can run this way for hours.
> How do I achieve this?


The whole idea is that a browser should do all this for you, so you
don't have to worry about it (ideally). I would advocate the use of
another computer language if you need this kind of low level memory
block access.

You could develop the gallery in Macromedia Flash, end let the SWF-file
handle its own memory objects (I believe this would stand apart from
the browser's memory allocation). I'm not sure if Flash/ActionScript
could go as low as influencing RAM on-the-fly. Maybe some Flash
Newsgroup could shed a light on this.

--
Bart

 
Reply With Quote
 
ivan.leben@gmail.com
Guest
Posts: n/a
 
      05-19-2006
The problem is I really need to achieve this in Firefox because I am
making an extension.

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-23-2006
(E-Mail Removed) wrote:

> Setting a pointer to null obviously won't delete the object pointed to
> from memory,


True, except of the "pointer" term. This is ECMAScript, not C; there are
references, not pointers.

The operation marks the object referred to for garbage collection, unless
there are other references to that object.

> but just change the address the pointer is referencing
> (from address of the object to null = 0x0000000).


Pure speculation.


PointedEars
--
Indiana Jones: The Name of God. Jehovah.
Professor Henry Jones: But in the Latin alphabet,
"Jehovah" begins with an "I".
Indiana Jones: J-...
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-23-2006
(E-Mail Removed) wrote:

> The problem is I really need to achieve this in Firefox because I am
> making an extension.


There is no need for preloading in the first place.


PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot Insert Preloaded Background Image into TR, TD in IE vunet.us@gmail.com Javascript 3 05-08-2007 02:08 PM
How can I disable HP preloaded datamining? warf Computer Security 13 12-15-2006 06:08 PM
My preloaded images are not preloaded anymore when ... marc557@juno.com Javascript 1 11-25-2006 05:36 AM
Preloaded XP and Explorer problem. Andrew Computer Support 7 12-19-2004 08:29 PM
password field can't be preloaded Homa ASP .Net 2 10-21-2003 01:28 AM



Advertisments