3

I tried to run this code to allocate a ArrayBuffer:

var data = new ArrayBuffer(336000000);
console.log(data.byteLength); // outputs 125829120

Does anyone know why the number if bytes is not allocated? Chrome don't give me any errors / warnings.

This problem is not on every platform, but on several like on Galaxy S6 (Android Browser)

  • 1
    That's 336 megabytes. Perhaps you only have access to 125 megabytes of memory? – Mmm Donuts Nov 23 '16 at 21:13
  • It outputs `336000000` just fine for me. I put it in the console on Chrome. – Neil Docherty Nov 23 '16 at 21:14
  • @NeilDocherty wouldn't it be dependent on the resources available? I'd expect you'd have more memory to utilize on your computer over a S6. – Mmm Donuts Nov 23 '16 at 21:15
  • @Kris, your right. I didn't even register that he wrote that he was on mobile. – Neil Docherty Nov 23 '16 at 21:17
  • 2
    Interesting, Firefox also does this. Not for that amount, but if I try to allocate 3GB it gives me only 1GB. Would have expected an exception instead of this. – Matthias247 Nov 23 '16 at 21:17
  • For reference, [here's the ArrayBuffer constructor in the ECMA 6 spec](http://www.ecma-international.org/ecma-262/6.0/#sec-arraybuffer-constructor) –  Nov 23 '16 at 21:30

1 Answers1

4

You're trying to allocate more memory than is available. However, according to the spec, this should raise an exception rather than returning you a smaller buffer:

http://www.ecma-international.org/ecma-262/6.0/#sec-createbytedatablock

Let db be a new Data Block value consisting of size bytes. If it is impossible to create such a Data Block, throw a RangeError exception.

And the previous spec https://www.khronos.org/registry/typedarray/specs/latest/#5

If the requested number of bytes could not be allocated an exception is raised

So if your browser is returning a smaller buffer rather than throwing an exception, it's non-compliant.

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
  • Look at the Compatibility array in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer. I think the question marks in the line about "new throws" is relevant. – Barmar Nov 23 '16 at 21:45
  • @Barmar my read is that that's just about the fact you're supposed to use `new ArrayBuffer(x)` rather than just `ArrayBuffer(x)`, and questioning whether the browsers allow the non-`new` version or throw an exception, i.e. not related to the size issue – CupawnTae Nov 23 '16 at 21:47