3

I'm using simple objects to wrap Image objects and track when they are loaded, like so:

var preloader = {
    loaded: false,
    image: new Image()
}
preloader.image.onload = function() {
    preloader.loaded = true;
}
preloader.image.src = 'http://www.example.com/image.jpg';

When the image is finished loading, preloader.loaded is set to true. That all works fine.

My question is, what happens when I have so many of these objects and so many images that the browser cache is used up. Eventually once enough images are loaded, the browser will start to dump older ones out of the cache. In that case won't I end up with JavaScript objects where loaded=true, but the image file is not actually cached anymore?

This is hard to test, because I can't tell at any given point which images are still in the cache and which aren't.

2
  • Are you referring to the in-RAM cache or the on-disk cache? Commented Sep 22, 2011 at 16:55
  • very good question from a theoretical standpoint, but practically speaking, @Kolink's answer is right Commented Sep 22, 2011 at 16:56

2 Answers 2

2

Once you've loaded the image from a web page, it's not being used from the cache. It's actually in the browser page memory for the duration of the browser page lifetime or until you get rid of this JS object so it can be garbage collected.

The cache is not used for storage of loaded images on a page. The cache is used so that the next time a load of this image is requested, it can be loaded much faster. If you load so many images that it exceeds the storage of the cache, nothing will happen to the images you have already loaded as they are in browser memory for the duration of the page (independent of the cache). What will happen is that some of the images you loaded will no longer be in the cache so the next time some browser pages wants to load them, they won't be in the cache and will have to be fetched from their original web-site over the internet.

Sign up to request clarification or add additional context in comments.

2 Comments

I see. So I've noticed that as I load more images, the memory usage will rise to a certain limit (around 300M in Chrome) and then drop down to about half. Does that mean that the images are still available, but just stored on the disk rather than in memory at that point?
Images loaded on a web page will remain in MEMORY for the duration of that web page. The only way they would ever get kicked out to disk is if the Operating System ran out of physical memory and used virtual paging to disk, but that would still report memory usage. If you saw browser memory usage go down, that would be for other reasons. Keep in mind that if you are loading lots of images via javascript, those images can be garbage collected (freed from memory and discarded) if you don't keep a reference to each image object in your javascript.
1

Yesterday I deleted 800MB of internet cache that had built up over the past few months. In short, I don't think it's possible to exhaust the browser cache unless the user has a really old machine, or you're preloading WAY too many images.

2 Comments

There are disk caches and memory caches. In most browsers, one can set the size of both of these, including setting a size of zero. They can easily be filled up, though filling up the cache has nothing to do with images that are already loaded for a web page - see my answer for details.
The answer still kind of applies to RAM though :p Thank you for the clarification. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.