如何在 Ajax 请求后释放内存 [英] How to free memory after an Ajax request

查看:49
本文介绍了如何在 Ajax 请求后释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个非常基本的 ajax 幻灯片.每次滚动时,新的图像和响应内容都会不断增加浏览器使用的内存量.

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.

我已经完成了我的研究并尝试了所有建议以在每个新请求上重置 XHR 对象,但这绝对没有帮助.

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?

谢谢,布赖恩

推荐答案

内存使用量增加是正常的.毕竟,您每次都加载更多数据——来自 AJAX 响应的 HTML,以及正在显示的图像.除非您使用 Adob​​e Pagemill 生成的 HTML,否则将只有几百字节的 HTML/文本.图像会占用最多的空间.一切都被塞进浏览器的缓存中.

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.

由于您没有直接对 DOM 做任何花哨的事情(构建子树等),只需重复替换一大块 HTML,最终浏览器将进行清理并丢弃一些未使用/旧/陈旧的图像内存/缓存中的数据并回收其中的一些内存.

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.

现在,如果您正在执行一些高度复杂的 DOM 操作并动态生成大量新节点,并且到处泄漏一些节点,那么您就会遇到内存问题,因为这些泄漏的节点最终会掩埋浏览器.

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.

这篇关于如何在 Ajax 请求后释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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