JQuery删除和内存泄漏 [英] JQuery Remove and memory leaks

查看:93
本文介绍了JQuery删除和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发游戏,我已经看到很多内存消耗,我使用jquery动画,在动画完成后,i。删除()元素,我的问题是,从dom树中删除元素后,对象仍然存在于内存中?

Im working on a game, and ive seen a lot of memory consuption, im using jquery animate, and after the animation is done, i .remove() the element, my question is, after removing an element from the dom tree, the object still exist in memory?

推荐答案

Javascript是一种垃圾收集语言。这意味着当没有代码保存对它的任何引用时,内存中的对象将被释放,并且(对于DOM对象)它不在DOM中。因此,当您从DOM中删除对象时,只要您的javascript的其他任何部分都没有对该DOM对象的引用,DOM对象将被清除,并且当垃圾收集器获取时,它的内存将返回到可用内存池。运行的机会。

Javascript is a garbage collected language. That means that an object in memory will be released when no code holds any references to it and (for a DOM object) it's not in the DOM. So, when you remove an object from the DOM, as long as no other part of your javascript has a reference to that DOM object, the DOM object will be cleaned up and it's memory returned to the available memory pool when the garbage collector gets a chance to run.

请记住,当垃圾收集器释放内存时,它可能不会立即返回系统。它可以作为分配给浏览器的内存保留,但它可供浏览器中的其他内存请求使用。因此,释放脚本中的内存不一定会使浏览器使用的总内存减少。

Keep in mind that when memory is freed by the garbage collector, it may not be returned to the system right away or ever. It may stay as memory allocated to the browser, but it will be available for use by other memory requests within the browser. So, freeing memory in your script won't necessarily make the total memory used by the browser go down.

如果重复执行相同的操作,那只是内存泄漏一遍又一遍地导致浏览器使用的总内存不断增加。只有这样才能确定某些内存被泄漏永久消耗。

It is only a memory leak if repeatedly carrying out the same operation over and over causes the total memory used by the browser to continually rise. Only then can you be sure that some memory is being permanently consumed by a "leak".

垃圾收集存在许多细微差别,特别是对于旧版本的IE ,但对于现代浏览器,您需要记住的主要是,如果您在自己的javascript数据结构中持有对象的引用,则不会对其进行垃圾回收。如果你没有引用它并且它不在DOM中,它将被释放并且它的内存被回收。

There are a number of nuances about garbage collection, particularly for older versions of IE, but for modern browsers, mostly what you need to keep in mind is that if you hold a reference to an object in your own javascript data structures, it will not be garbage collected. If you don't hold a reference to it and it's not in the DOM, it will be freed and its memory recycled.

这篇关于JQuery删除和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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