且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Ajax 请求后释放内存

更新时间:2023-02-02 21:13:57

内存使用量增加是正常的.毕竟,您每次都加载更多数据——来自 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.