nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

NW.js ignores max-old-space-size

Open aggregate1166877 opened this issue 3 years ago • 2 comments

Version / platform

NWJS Version : v0.59.1 Operating System : Windows

Expected behavior

NW.js should support more than 4GB heap when specifying --max-old-space-size=XX in js-flags like Node.js does.

Actual behavior

Only values under 4GB are reflected.

How to reproduce

First off, I just want to thank the maintainers for all their awesome work <3

It appears NW.js does not support heaps larger than 4GB. I found this issue, which claims all should be working. However, it seems that NW.js allows reducing heap size but does not allow increasing heap size. I have 64GB RAM in my machine, so it's not a hardware issue.

Below I demonstrate that increasing the heap via --max-old-space-size works fine with Node.js, but does not work in NW.js. I use 16GB memory in my examples, but 8GB RAM suffers the same 4GB limit issue.

Node.js Download latest x64 Node.js LTS version (I'm using v17.3.0).

Create an index.js file:

const v8 = require('v8');
const heapSize = ((v8.getHeapStatistics().heap_size_limit / (1024 * 1024 * 1024)).toFixed(2));
console.log(`Max heap size: ${heapSize}GB`);
// ^^ should display ~16.00GB.

let count = 0;
let parentArrayI = 0;
let parentArray = [[]];
let i = 0;

while (true) {
  parentArray[parentArrayI].push(i);
  if (i % 10000000 === 0) {
    console.log(`[${count++}] at`, count, 'ticks');
    parentArray.push([]);
    parentArrayI++;
    i = 0;
  }

  i++;
}

Run it:

node --max-old-space-size=16384 index.js

The console prints Max heap size: 16.05GB. In my task manager, Node.js consumes 16GB memory before finally reaching '136 ticks' and crashing, as expected.

NW.js Download latest x64 SDK version, v0.59.1 (also tested with 0.55.0).

Create an empty index.html.

Create a package.json with:

{
	"name": "memory_example",
	"main": "index.html",
	"js-flags": "--max-old-space-size=16384"
}

Note: the package.json's js-flags is not the only way to do this - you should also be able to set an environment variable with : NODE_OPTIONS=--max_old_space_size=16384 (neither option worked for me).

Run nw.exe, and press F12 to open DevTools. Paste the contents of index.js above into the console, press enter. The logs will print:

Max heap size: 4.00GB

Watch the task manager. At 3.5GB, a break point stops execution. If you force it to proceed, the app stops at 62 ticks with "Out of memory" (specifically, the Chrome crashed tab page shows).

aggregate1166877 avatar Jan 02 '22 06:01 aggregate1166877

That space is limited to 4GB when V8 pointer compression is turned on in the build, which is now the default in Chromium. See also the Node discussion: https://github.com/nodejs/TSC/issues/790

rogerwang avatar Jan 14 '22 03:01 rogerwang

Thank you for the link @rogerwang, very interesting post. It's kind of unfortunate that it's baked in - I'm making a 3D video game in NW.js with high detail models and massive datasets, and I've recently starting tipping over that limit.

Was kind of hoping to avoid compiling my own setups. It's not something I really do, and I'd be losing the massive testing community NW.js has.

aggregate1166877 avatar Jan 14 '22 13:01 aggregate1166877

Closing this for now. Feel free to post any questions related to building NW.js on the mailing list.

ayushmanchhabra avatar Oct 20 '22 04:10 ayushmanchhabra