Skip to content

Instantly share code, notes, and snippets.

@vicapow
Last active November 5, 2015 04:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vicapow/ee9f5f8180b39b438072 to your computer and use it in GitHub Desktop.
Save vicapow/ee9f5f8180b39b438072 to your computer and use it in GitHub Desktop.
Was I really just able to allocate 56GB of memory in Google chrome?
var x = [];
for (var i = 0; i < 56; i++) {
x.push(new Uint8Array(1024 * 1024 * 1024));
}
console.log(x.length);
@vicapow
Copy link
Author

vicapow commented Oct 17, 2015

@icepic are you saying this is happening by the browser? Or at the OS level? https://twitter.com/vicapow/status/655248145316642816?s=17

@vicapow
Copy link
Author

vicapow commented Oct 17, 2015

Here, I allocate 50GB, assign each byte a random value [0, 256) and accumulate the result. One downside, as far as I can tell, you can't allocate a single TypedArray larger than 1GB.

This took ~23min to run but didn't crash!

var t0 = Date.now();
var x = [];
var GB1 = 1024 * 1024 * 1024;
var i;
var j;
var gigs = 50;
for (i = 0; i < gigs; i++) {
  x.push(new Uint8Array(GB1));
  for (j = 0; j < GB1; j++) {
    x[i][j] = Math.random() * 256 | 0;
  }
  console.log('gig', i);
  console.log('t', Date.now() - t0);
}

i = 0;
var accum = 0;
for (i = 0; i < gigs; i++) {
  for(j = 0; j < GB1; j++) {
    accum = accum + x[i][j] - 128;
  }
  console.log('gig', i);
  console.log('t', Date.now() - t0);
}

console.log('accum', accum);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment