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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745502070a4630461.html
评论列表(0条)