使用Javascript + IMG标签=内存泄漏。有没有更好的办法做到这一点? [英] Javascript + IMG tags = memory leak. Is there a better way to do this?
问题描述
我已经得到了多数民众赞成使用jQuery获得一定的产品信息,人们看待事物的网页,然后显示被视为最后的产品图片。这是一个jQuery AJAX回调,看起来pretty的多是这样的:
I've got a web page that's using jquery to receive some product information as people are looking at things and then displays the last product images that were seen. This is in a jquery AJAX callback that looks pretty much like this:
if(number_of_things_seen > 10) {
$('#shots li:last-child').remove();
}
$('<li><img src="' + p.ProductImageSmall + '"></li>').prependTo('#shots');
不过,这似乎漏了相当多的内存。从外观上看,它确实是正确的,但足迹无限增长。
However, it seems to leak quite a bit of memory. Visually, it does the right thing, but the footprint grows indefinitely.
Safari浏览器的DOM Inspector显示的DOM是我会希望它是,但它似乎保持引用的每一个形象,它已显示出来(如在的在任何情况下,这个截图感兴趣)。
Safari's DOM inspector shows the DOM is how I would expect it to be, but it seems to maintain references to every image that it has displayed (as seen in this screenshot in case anyone is interested).
我添加了
$('#shots li:last-child img').remove();
要删除的语句没有noticable效果。
to the removal statement to no noticable effect.
有没有必要让浏览器的一些魔法释放一些这方面的东西?
Is there some magic necessary to let the browser release some of this stuff?
推荐答案
浏览器是臭名昭著的内存泄漏。这听起来像网页时保持运行相当长的时间内出现的问题。如何刷新页面就内存用完过吗?
Browsers are notorious for memory leaks. It sounds like the problem occurs when the page is left running for a long time. How about refreshing the page before it runs out of memory?
window.setTimeout("location.reload()",1000*60*60);//refresh in an hour
这篇关于使用Javascript + IMG标签=内存泄漏。有没有更好的办法做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!