如何在 JavaScript 中释放内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8467350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:06:51  来源:igfitidea点击:

How to free up the memory in JavaScript

javascriptarraysmemory-managementhtml5-canvas

提问by haynar

I'm working with canvas and its ImageData object which contains a huge amount of data (millions of integers). So working with a few arrays already takes a lot of memory (up to 300MB). Is there a way to free up the memory of some array when it's unnecessary? I'm trying to assign undefinedto that variable. Is it right?

我正在使用画布及其 ImageData 对象,其中包含大量数据(数百万个整数)。因此,使用几个数组已经需要大量内存(高达 300MB)。有没有办法在不需要时释放某些数组的内存?我正在尝试分配undefined给该变量。这样对吗?

回答by jfriend00

If the variable persists (e.g. it's global or part of some persistent data structure) and the data it points to is large and you want that data to be eligible for garbage collection, then you are correct to assign something small to that variable. undefinedor nullor ""will all work. What you're doing is clearing the reference to the large data so that it will be eligible for garbage collection. If nothing else in your javascript has a reference to that data, then it can be freed by the garbage collector. If anything else has a reference to it, then it cannot be freed.

如果变量持续存在(例如,它是全局的或某些持久数据结构的一部分)并且它指向的数据很大,并且您希望该数据有资格进行垃圾回收,那么您将一些小的值分配给该变量是正确的。 undefinednull""将全部工作。您正在做的是清除对大数据的引用,以便它有资格进行垃圾收集。如果您的 javascript 中没有其他内容引用该数据,则垃圾收集器可以将其释放。如果其他任何东西都引用了它,那么它就不能被释放。

For example, if you had a 10,000 element array held in a global variable:

例如,如果您在全局变量中保存了一个 10,000 个元素的数组:

var largeDataArray = new Array(10000);

And, you had filled most elements with data, then you could allow that memory to be eligible for garbage collection by assigning it some other value like:

而且,您已经用数据填充了大多数元素,然后您可以通过为其分配一些其他值来允许该内存有资格进行垃圾回收,例如:

largeDataArray = null;

or if you still want it to be an array:

或者如果你仍然希望它是一个数组:

largeDataArray = [];

Note: variables that themselves go out of scope (like local variables in functions that aren't part of a lasting closure) or variables in objects that themselves go out of scope do not have to be manually cleared. When they go out of scope or when the parent object is deleted, the data contained within will also be eligible for garbage collection.

注意:本身超出范围的变量(如函数中的局部变量不是持久闭包的一部分)或对象中的变量本身超出范围,不必手动清除。当它们超出范围或父对象被删除时,其中包含的数据也将有资格进行垃圾回收。

So, the clearing of a variable only needs to be done when you explicitly want to free data that is held in a long lasting variable and it's usually only relevant to worry about this when the data is large or you have a lot of them that add up to multiple megabytes of data (memory use is of higher concern at lower levels on smartphones than in desktop browsers).

因此,只有当您明确想要释放保存在持久变量中的数据时才需要清除变量,并且通常只有在数据很大或者您有很多添加的数据时才需要担心这一点高达数兆字节的数据(与桌面浏览器相比,智能手机上的内存使用在较低级别上受到更高的关注)。

回答by Matt Ball

JavaScript has automatic memory management. Memory containing objects which are no longer referenced will be eligible for garbage collection, unless you have a memory leak. There is generally no need to manually assign undefinedto variables.

JavaScript 具有自动内存管理功能。包含不再被引用的对象的内存将有资格进行垃圾回收,除非您有内存泄漏。通常不需要手动分配undefined给变量。

If your program is using too much memory, you should shrinkthe arrays to get rid of elements you no longer need. See Array.pop, Array.shift, and Array.splice.

如果您的程序使用了太多内存,您应该缩小数组以去除不再需要的元素。见Array.popArray.shift、 和Array.splice