Ajax 请求后如何释放内存
我的网站上有一个非常基本的 ajax 幻灯片。在每次滚动时,新图像和响应内容都会不断增加浏览器使用的内存量。
我已经完成了研究并尝试了所有建议来重置每个新请求的 XHR 对象,但这绝对没有任何帮助。
幻灯片很基本,但可能包含数百张幻灯片。我希望用户能够无限期地浏览幻灯片而不会使浏览器崩溃。这可能吗?
谢谢,布莱恩
I have a very basic ajax slideshow on my website. On every scroll, the new images and response content continually increase the amount of memory used by the browser.
I've done my research and tried all suggestions to reset the XHR object on each new request, but this does absolutely nothing to help.
The slideshows are basic but may contain hundreds of slides. I want a user to be able to navigate the slideshow indefinitely without crashing their browser. Is this even possible?
Thanks, Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内存使用量增加是正常的。毕竟,您每次都会加载更多数据 - 来自 AJAX 响应的 HTML 以及正在显示的图像。除非您使用 Adobe Pagemill 生成的 HTML,否则这只是几百字节的 HTML/文本。图像会占用最多的空间。所有内容都会被填充到浏览器的缓存中。
由于您没有直接对 DOM 做任何花哨的事情(构建子树之类的),只是重复替换一大块 HTML,最终浏览器将进行清理并从内存中丢弃一些未使用/旧/过时的图像数据/cache 并回收一些内存。
现在,如果您正在进行一些高度复杂的 DOM 操作并动态生成大量新节点,并且到处泄漏一些节点,那么您就会遇到内存问题,因为这些泄漏的节点最终会埋葬浏览器。
但是,仅仅通过加载图像来增加内存使用量就不用担心,它就像正常的扩展冲浪会话一样,只不过您只是加载一些新图片。
Increasing memory usage is normal. You are, after all, loading more data each time - the HTML from your AJAX response, as well as the images that are being displayed. Unless you're using Adobe Pagemill-generated HTML, that's only going to be a few hundreds of bytes of HTML/text. It's the images that will suck up the most space. Everything get stuffed into the browser's cache.
Since you're not doing anything fancy with the DOM (building sub-trees and whatnot) directly, just replacing a chunk of HTML repetitively, eventually the browser will do a cleanup and chuck some of the unused/old/stale image data from memory/cache and reclaim some of that memory.
Now, if you were doing some highly complex DOM manipulations and generating lots of new nodes on the fly, and were leaking some nodes here and there, THEN you'd have a memory problem, as those leaked nodes will eventually bury the browser.
But, just increasing memory usage by loading images is nothing to worry about, it's just like a normal extended surfing session, except you're just loading some new pictures.
如果是幻灯片放映,您一次只显示一张图像吗?如果您一次只显示一个,并且永远不会删除最后一个显示的内容,那么它总是会增加内存。如果您删除未显示的幻灯片,应该会有所帮助。
If its a slideshow, are you only showing one image at a time? If you do only show one at a time and you're never getting rid of the last one you show, it will always increase the memory. If you remove the slides not being shown, it should help.