我今天遇到一个奇怪的问题。对于其他人来说,这可能是一个简单的答案,但这让我感到困惑。为什么下面的代码会导致内存错误?
var cur = 167772160; var bcast = 184549375; var addresses = []; while (cur <= bcast){ cur += 1; addresses.push(cur); } addresses.length addresses // memory goes from a few megs to over a gig in seconds when trying to print this
我得到了这两个错误之一…第一个是在节点的解释器中运行此代码时,第二个是通过nodeunit运行它时:
严重错误:CALL_AND_RETRY_2分配失败-内存不足
严重错误:JS分配失败-内存不足
当我尝试访问阵列时会发生这种情况。但是获取长度却没有。
> var cur = 167772160; > var bcast = 184549375; > var addresses = []; > while (cur <= bcast){ ... cur += 1; ... addresses.push(cur); ... } 16777216 > addresses.length 16777216 > addresses FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory