0

I found some issue with .length = 0 and now if I change to = [], all things are working well in my javascript project.

var arr = [1,2,3,4,5];
alert(arr);
arr = [];
alert(arr);
arr.length = 0;
alert(arr);

But I have one question if I use arr = [], when the memory of [1,2,3,4,5] disappear? If the array is big and I use [] lots of times it will cause memory lack problems.

2
  • "I found some issue with .length = 0" Care to share? Commented Aug 30, 2017 at 15:55
  • You can see my stack problem stackoverflow.com/questions/45924788/… here I changed .length =0 to = [] and solved the problem Commented Aug 30, 2017 at 15:57

2 Answers 2

0

But I have one question if I use arr = [], when the memory of [1,2,3,4,5] disappear?

Whenever the JavaScript engine gets around to releasing it, which it will do as and when necessary once nothing has a reference to it anymore. The details vary depending not only on the JavaScript engine involved, but on where that code appears and how frequently it's run (e.g., how aggressively it ends up getting optimized). If it matters (e.g., that's run a lot), it'll get reclaimed really quickly.

Sign up to request clarification or add additional context in comments.

7 Comments

I want to you to describe why this post is available stackoverflow.com/questions/4804235/… according to your answer.
@artgb: I don't understand the question in your comment...?
You said if I use arr = [] the memory space will disappear automatically and why .length = 0 is needed?
@artgb: If you've done arr = [], you don't need arr.length = 0.
Thanks, I understood your idea just now
|
0

Your question comes down to how the various Javascript engines perform garbage collection. While they each do it slightly differently, the key to allowing them cleanup allocated memory is to make sure there are no lingering references (including through a closure or async call) remaining to the original array.

Gets trickier if that array has been passed into another function that is still on the stack or itself in a closure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.