简体   繁体   English

我需要保留对预加载图像的引用,还是可以对其进行垃圾回收?

[英]Do I need to keep a reference to preloaded images or can they be garbage collected?

Every example of image preloaders that I find in books or online contains some form of a cache array of all the preloaded images. 我在书中或在线上找到的每个图像预加载器示例都包含某种形式的所有预加载图像的cache阵列。

I've created a DEMO page that if you open the console, you can clearly see it preloads images even if they get garbage collected. 我创建了一个DEMO页面 ,如果您打开控制台,您可以清楚地看到它预加载了图像,即使它们被垃圾收集了也是如此。

Is there any reason why all the other preloaders keep a reference to the preloaded images, or is it unnecessary? 是否有其他所有预加载器保留对预加载映像的引用的原因,还是不必要?

In general, it's not useful to keep a cache of loaded images since the browser already do that automatically. 通常,保留已加载图像的缓存是没有用的,因为浏览器已经自动执行此操作。 But if those libraries you mentioned do it, they might have a reason. 但是,如果您提到的那些库做到了,则可能是有原因的。 Maybe for animation purposes since loading the image from JavaScript memory might be faster than loading it from the browser cache. 也许出于动画目的,因为从JavaScript内存加载图像可能比从浏览器缓存加载图像更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM