在javascript中释放数组数组的最佳方法 [英] Best way to deallocate an array of array in javascript

查看:180
本文介绍了在javascript中释放数组数组的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中释放数组数组的最佳方法是什么,以确保不会发生内存泄漏?

What is the best way to deallocate an array of array in javascript to make sure no memory leaks will happen?

var foo = new Array();
foo[0] = new Array();
foo[0][0] = 'bar0';
foo[0][1] = 'bar1';
foo[1] = new Array();
...




  1. 删除(foo)?

  2. 迭代foo,删除(foo [index])和删除(foo)?

  3. 1和2给我相同的结果?

  4. 无?

  1. delete(foo)?
  2. iterate through foo, delete(foo[index]) and delete(foo)?
  3. 1 and 2 give me the same result?
  4. none?


推荐答案

foo = null;

应该足以让垃圾收集器摆脱数组,包括它的所有子数组(假设没有别的东西可以参考他们)。请注意,只有当它想要时才会消除它,而不是立即消除它,所以如果浏览器的内存消耗没有立即消失,不要感到惊讶:这不是泄漏。

should be enough for the garbage collector to get rid of the array, including all its child arrays (assuming nothing else has a reference to them). Note that it will only get rid of it when it wants to, not immediately, so don't be surprised if the browser's memory consumption doesn't drop straight away: that isn't a leak.

如果任何这些数组元素包含对DOM节点的引用,它可能会变得更复杂。

It potentially gets more complicated if any of those array elements contain references to DOM nodes.

这篇关于在javascript中释放数组数组的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆