Increasing max-old-space-size in node.js app running as Windows service

912 Views Asked by At

I'm relatively new to node.js and have built an app that is designed to be deployed as a standalone application without the need to install node.js or any of its dependencies.

I'm running into an issue where in specific, not easily reproducible instances, the app crashes and the I get a "Javascript heap out of memory" error.

While I'm debugging the root cause, I'm trying to increase the heap size. I'm using node-windows to run the application as a service and nexe to build. I've tried a great number of things I've found in other forum responses including:

  • Various version of Python 2x & 3x
  • Running "build": "nexe --enableNodeCli --build" in the build script and adding --max-old-space-size=4096 to the shortcut target
  • Ugrading (and eventually rolling back) Node.js
  • Adding --max-old-space-size=4096 to NODE_OPTIONS in Windows environment variables

..among others. These just seem to be the most common solutions suggested.

Some of these solutions resulted in an inability to run the build. Others run, however the memory remains at around 1.5 GB.

Has anyone come up against this issue and found a solution that increases heap memory for an app that:

  1. Runs in an environment without Node
  2. Runs as a Windows service

......

  • OS: Windows 10
  • Node: 10.22.1 (have also tried 12.19.0)
  • Nexe: 3.3.7
  • Python: 2.7.17 (have also tried 2.7.16, 2.7.14, 3.7, 3.8 & 3.9)

......

EDIT: I tried adding nodeOptions: ['--max-old-space-size=4096'] into the service options, but still get feedback from v8.getHeapStatistics that the size is around 1.42 GB. I've been creating the service within index.js and building it into the executable so far (to avoid having to install node.js) but I did separate it out into its own script and ran it through the console - same result.

Here's the code:

    const fs = require('fs');
    const v8 = require('v8');
    
    const Service = require('node-windows').Service
    var svc = new Service({
        name: 'VDE',
        description: 'VDE',
        script: __dirname + '/index.js',
        execPath: __dirname + '/VDE.exe',
        abortOnError: false,
        nodeOptions: [
            '--max-old-space-size=4096'
        ]
    })
    
    var EventLogger = require('node-windows').EventLogger;
    var log = new EventLogger('VDE Event Log');
     
    svc.on('install',function(){
        svc.start();
    });
    
    svc.on('start',function(){
        log.info(`VDE is running.`);
        checkUsage()
    })
    
    svc.on('error',function(err){
        log.error(`Error: ${err}`);
    })
    
    svc.on('stop',function(){
        log.warn(`Service has stopped.`);
    
        svc.start();
    })
    
    if (svc.exists == false){
        svc.install();
        log.info(`Windows service for VDE does not exist. Installing now....`);
    }else{
        log.info(`Windows service for VDE already exists!`);
        svc.start();
    };
    
    function checkUsage(){
        let heapSize = (v8.getHeapStatistics().heap_size_limit / 1024 / 1024 / 1024).toFixed(2);
    
        log.info(`Heap: is ~${heapSize} GB`);
    }
1

There are 1 best solutions below

8
On

You can provide node-windows some options where you are able to set the variable --max-old-space-size.

const options= {
    name: 'Service',
    description: 'Service',
    nodeOptions: [
        '--max-old-space-size=4096'
    ]
}
const service = require('node-windows').Service;
const svc = new Service(options);