3

For example:

var test = [1,2,3,4,5];
test.length = 0;

Is this a good way to release memory of variable test?

Lee Taylor
  • 7,155
  • 14
  • 29
  • 44
ZHENG Wen
  • 31
  • 2
  • Not quite sure what you mean, but it you could do `var test = [1,2,3]` and then `test = []` – Foreign Object May 09 '13 at 16:18
  • Use delete test instead of that – Deepu May 09 '13 at 16:18
  • 1
    @Deepu `delete` does not work on variables. This post may be of interest to you: http://perfectionkills.com/understanding-delete/ – Rob W May 09 '13 at 16:19
  • Yes, this will clear the array and all references to it. It's preferable to test = [] as that will create a new array, and may not replace all references to the original array. – MasNotsram May 09 '13 at 16:19
  • 1
    Also, this may be worth a read: http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript – MasNotsram May 09 '13 at 16:19

1 Answers1

2

No don't worry about that, that just makes your code longer. Keep your variable in the tightest of scope, and if not needed anymore, it should get handled by the garbage collection system.

dchhetri
  • 6,590
  • 4
  • 39
  • 55