nodejs中的缓冲区大小 [英] Buffer size in nodejs

查看:498
本文介绍了nodejs中的缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// node v0.5.6 //

// node v0.5.6 //

我假设nodejs可以在nodejs堆之外分配的最大缓冲区大小受可用系统内存量的限制。我有几次免费内存但我似乎无法在没有崩溃节点的情况下接近该限制。

I assume the max buffer size that nodejs can allocate outside of the nodejs heap is limited by the amount of available system memory. I have several gigs of free memory though and I can't seem to get even close to that limit without crashing node.

致命错误:JS分配失败 - 处理掉内存

FATAL ERROR: JS Allocation failed - process out of memory

function bigArray(){
  // each ip could be 10 digits long, therefore,
  // 10 * (bcast-cur) = size of Buffer.  
  // does that also mean size in bytes? 
  var cur = 167772160;
  var bcast = 184549375;
  var addresses = new Buffer((bcast-cur)*10);
  var offset = 0;
  while (cur <= bcast){
    cur += 1;
    addresses.writeUInt32LE(cur,offset);
    offset+=10;
  }
  return addresses;
};
var ba = bigArray();

它在此块的节点库中的Buffer.js的第235行崩溃:

It crashes on line 235 of Buffer.js in the node library at this block:

if (this.length > Buffer.poolSize) {
  // Big buffer, just alloc one.
  this.parent = new SlowBuffer(this.length); //crash here
  this.offset = 0;


推荐答案

您收到的错误消息有点误导不幸的是,但是你有一个缓冲区溢出错误。

The error message that you are getting is a bit misleading unfortunately, but you have a buffer overflow error.

你的循环将一直运行直到cur == bcast,所以最后一个writeUInt32LE会写一个超过缓冲区长度的数字。将你的循环比较改为cur< bcast。

Your loop will run until cur == bcast, so the very last writeUInt32LE will write a number past the length of the buffer. Change your loop comparison to "cur < bcast".

这篇关于nodejs中的缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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