admin管理员组文章数量:1429486
I am working with image processing with canvas.
Users will select 10 photos (normally large-resolution photos) each time, then a script will process these photos before uploading them to a server.
The process will create large arrays and consumes lots of memory. So I am wondering which of the options listed below is the correct way to free memory.
option 1: do nothing, leave GC to handle the memory use
for(int i=0;i<10;i++) {
var bigArray = new Array(20000000);
//do something with bigArray
}
option 2: set to null
for(int i=0;i<10;i++) {
var bigArray = new Array(20000000);
//do something with bigArray
bigArray=null;
}
option 3: set to null after freeing array contents
for(int i=0;i<10;i++) {
var bigArray=new Array(20000000);
//do something with bigArray
bigArray=[];
bigArray=null;
}
option 4: set to null after setting array length to zero
for(int i=0;i<10;i++) {
var bigArray=new Array(20000000);
//do something with bigArray
bigArray.length=0;
bigArray=null;
}
I am working with image processing with canvas.
Users will select 10 photos (normally large-resolution photos) each time, then a script will process these photos before uploading them to a server.
The process will create large arrays and consumes lots of memory. So I am wondering which of the options listed below is the correct way to free memory.
option 1: do nothing, leave GC to handle the memory use
for(int i=0;i<10;i++) {
var bigArray = new Array(20000000);
//do something with bigArray
}
option 2: set to null
for(int i=0;i<10;i++) {
var bigArray = new Array(20000000);
//do something with bigArray
bigArray=null;
}
option 3: set to null after freeing array contents
for(int i=0;i<10;i++) {
var bigArray=new Array(20000000);
//do something with bigArray
bigArray=[];
bigArray=null;
}
option 4: set to null after setting array length to zero
for(int i=0;i<10;i++) {
var bigArray=new Array(20000000);
//do something with bigArray
bigArray.length=0;
bigArray=null;
}
Share
Improve this question
edited Mar 14, 2019 at 15:33
squaleLis
6,4942 gold badges25 silver badges31 bronze badges
asked Mar 14, 2019 at 15:24
xw uwoxw uwo
934 bronze badges
2
- setting it to null is all that is required. The other options (assigning empty array, etc) provide no benefit as the heap will be cleared at next GC anyway. – Randy Casburn Commented Mar 14, 2019 at 15:26
- Thank you for your answer. Just tested, bigArray = []; bigArray = null; and bigArray.length = 0; all work fine to free the large array properly. According to your answer, do you mean bigArray = null will be the preferable one between above three methods? – xw uwo Commented Mar 14, 2019 at 17:06
3 Answers
Reset to default 3bigArray
will get garbage collected pletely if all references to it are lost. With:
bigArray = [];
the original reference gets lost, and the array gets garbage collected, bigArray
points to a new empty array. With:
bigArray = null;
the reference gets lost too. With
bigArray.length = 0;
the array will loose the reference to all its stored values, just the array itself stays in memory.
Now what you should do (instead of creating a leaking a variable outside of a loop and manually manage its dereferencing):
Just define the scope properly, so that it will automatically gets unreferenced:
for(let i = 0; i < 10; i++) {
let bigArray = new Array(20000000);
//do something with bigArray
//...
// array gets recycled here
}
Sidenote:
Array(20000000)
is not a big array until you fill it with actual values.
Setting it to null or setting it to an empty array, undefined empty string or other option will all work as it make data eligible for garbage collection.
Notice:
In your first example (using var
) will not make it eligible for garbage collection as you are storing a reference from the global scope to your var
:
for(i=0;i<10; i++){
var pippo = "TEST";
}
console.log(pippo);
Hi,
bigArray = null;
if you want to reuse the variable for an other type
bigArray = [];
if you want to reuse it as array
delete bigArray;
if you've done with this variable
本文标签: javascriptWhat is the correct way to free large array memoryStack Overflow
版权声明:本文标题:javascript - What is the correct way to free large array memory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745502069a2661077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论