Max Uint8Array length


Test for v6.10.1:

D:\AppServ\www\test\nodejs>node -v
v6.10.1

D:\AppServ\www\test\nodejs>node
> new Uint8Array(1 << 30 - 5)

<--- Last few GCs --->

   12990 ms: Mark-sweep 1367.6 (1396.1) -> 1333.6 (1361.4) MB, 469.2 / 0.0 ms [a
llocation failure] [GC in old space requested].
   13556 ms: Mark-sweep 1333.6 (1361.4) -> 1333.6 (1361.4) MB, 565.7 / 0.0 ms [a
llocation failure] [GC in old space requested].
   14132 ms: Mark-sweep 1333.6 (1361.4) -> 1333.6 (1357.4) MB, 575.9 / 0.0 ms [l
ast resort gc].
   14693 ms: Mark-sweep 1333.6 (1357.4) -> 1333.6 (1357.4) MB, 560.9 / 0.0 ms [l
ast resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0000003A5F4CFB49 <JS Object>
    1: arrayToHash(aka arrayToHash) [util.js:~252] [pc=00000050E7B3E5C5] (this=0
000003A5F404381 <undefined>,array=000002A57819ED51 <JS Array[33554432]>)
    2: formatValue(aka formatValue) [util.js:370] [pc=00000050E7CA9513] (this=00
00003A5F404381 <undefined>,ctx=000002A57819EA49 <an Object with map 0000037323D3
1541>,value=000002A57819E539 <an Uint8Array with map 000000C929008039>,recurseTi
me...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memo
ry

So, we have the result:

// Node.js v6.10.1

new Uint8Array(1 << 30 - 6)

// Node.js v8.9.0
// NOTE: `new Uint8Array(1 << 30 - 4)` crashes Node.js, but doesn't throw `RangeError`

new Uint8Array(1 << 30 - 5)

// Chrome Canary Version 65
// Google Chrome Version 66.0.3359.139 (Official Build) (64-bit)

new Uint8Array(((1 << 30) * 2) - (1 << 12) - 1);

Leave a Reply