Node.js 缓冲区的最大大小是多少

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8974375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:03:18  来源:igfitidea点击:

What's the maximum size of a Node.js Buffer

node.jsv8

提问by Joel

I got a fatal error reading a file was too big to fit in a buffer.

我在读取文件太大而无法放入缓冲区时遇到致命错误。

FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData() length exceeds max acceptable value

Or,

或者,

RangeError: "size" argument must not be larger than 2147483647 at Function.Buffer.allocUnsafe (buffer.js:209:3)

RangeError: "size" 参数在 Function.Buffer.allocUnsafe (buffer.js:209:3) 处不得大于 2147483647

If I try to allocate a 1GB Buffer I get the same fatal Error,

如果我尝试分配 1GB 缓冲区,我会得到同样的致命错误,

var oneGigInBytes = 1073741824;
var my1GBuffer = new Buffer(oneGigInBytes); //Crash   

What is the maximum size of a Node.js Buffer class instance?

Node.js Buffer 类实例的最大大小是多少?

采纳答案by Vyacheslav Egorov

Maximum length of a typed array in V8 is currently set to kSmiMaxValuewhich depending on the platform is either:

V8 中类型化数组的最大长度当前设置为kSmiMaxValue取决于平台:

  • 1Gb - 1byte on 32-bit
  • 2Gb - 1byte on 64-bit
  • 1Gb - 32 位 1 字节
  • 2Gb - 64 位 1 字节

Relevant constant in the code is v8::internal::JSTypedArray::kMaxLength(source).

代码中的相关常量是v8::internal::JSTypedArray::kMaxLengthsource)。

V8 team is working on increasing this even further on 64-bit platforms, where currently ArrayBufferobjects can be up to Number.MAX_SAFE_INTEGERlarge (2**53 - 1). See bug 4153.

V8 团队正在努力在 64 位平台上进一步增加这一点,目前ArrayBuffer对象可以达到Number.MAX_SAFE_INTEGER大 (2**53 - 1)。请参阅错误 4153

回答by Evan Carroll

This is now documented as part of Node's bufferapi, the maximum size is buffer.constants.MAX_LENGTH.

这现在被记录为Node 的bufferapi 的一部分,最大大小为buffer.constants.MAX_LENGTH.

buffer.constants.MAX_LENGTH<integer>The largest size allowed for a single Buffer instance.

  • On 32-bit architectures, this value is (2^30)-1(~1GB).
  • On 64-bit architectures, this value is (2^31)-1(~2GB).

This value is also available as buffer.kMaxLength.

buffer.constants.MAX_LENGTH<integer>单个 Buffer 实例允许的最大大小。

  • 在 32 位架构上,此值为(2^30)-1(~1GB)。
  • 在 64 位架构上,此值为(2^31)-1(~2GB)。

此值也可用作buffer.kMaxLength.

So you can figure out how big it is by doing

所以你可以通过做来弄清楚它有多大

> (require('buffer').constants.MAX_LENGTH + 1) / 2**30
2

回答by Anthony

Seems like the current max buffer size is 2147483647 bytes aka 2.147GB

似乎当前的最大缓冲区大小是 2147483647 字节,也就是 2.147GB

Source: https://stackoverflow.com/a/44994896/3973137(and my own code)

来源:https: //stackoverflow.com/a/44994896/3973137(和我自己的代码)