1

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 Answers1

0

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);
MaatMa
  • 204
  • 1
  • 7
  • Lo and behold there it is in the node-windows documentation: // Create a new service object var svc = new Service({ name:'Hello World', description: 'The nodejs.org example web server.', script: 'C:\\path\\to\\helloworld.js', nodeOptions: [ '--max_old_space_size=4096' ] //, workingDirectory: '...' //, allowServiceLogon: true }); – kiss_my_patch Oct 22 '20 at 15:34
  • I just added this to my script, but when I start the windows service I get the same result. Heap: is ~1.42 GB. I'm tracking my heap size using v8.getHeapStatistics and writing to a log when the service starts: const used = process.memoryUsage(); let heapSize = (v8.getHeapStatistics().heap_size_limit / 1024 / 1024 / 1024).toFixed(2); Full discretion, I am creating the service as part of my index.js script which I compile to an .exe to avoid needing to run node commands in the console. If there's a better way, let me know. – kiss_my_patch Oct 22 '20 at 15:36
  • I hope you didn't used my example because there was a mistake one minute ago. Can you try to run it without the exe file and just install the service with the console? After that there should be a folder called "deamon" and a ".xml" file. In this file should be your --max-old-space-size option. – MaatMa Oct 23 '20 at 06:00
  • When I do that, I do see the max-old-space-size option in the XML. But in the (out.log where I'm printing console logs from index.js) and in the Event Viewer where I'm printing service logs, I still see that heap = ~1.42GB. Is it possible that feedback is inaccurate? – kiss_my_patch Oct 23 '20 at 13:46
  • Did you tried to run it without your .exe file? I cant tell if the error is inaccurate. Normally the default maximum size should be 512mb so it could had an effect allready. – MaatMa Oct 26 '20 at 06:51
  • Correct, this is without the .exe file. I installed the service by running the js file in command line. I'm setting max-old-space-size to 4096, so 1.42 GB is not correct. In fact, when I run the entire index script from cli, the heap increases to 4.10 GB. However, even when installing the windows server using this method and including max-old-space-size=4096 in the nodeOptions, I'm always seeing 1.42 GB. – kiss_my_patch Oct 26 '20 at 14:14
  • Could it be that the v8 with what youre checking only got 1.42GB because it is not running in the service context? But from now on I don't know exactly what to test anymore. I can tell you that my application is running. I have the newest node-windows version and nodejs 12. I testet it by forcing the memory getting to big and changed the max-old-space-size after. – MaatMa Oct 27 '20 at 06:51
  • I am also forcing memory now. I had suspected that perhaps the v8 feedback wasn't accurate, but forcing it to max out debunked that. I've tried both running the app as a exe and a windows service - same thing. I'll keep working on it and let update this thread if I'm able to resolve it. Thank you for all of your help so far. – kiss_my_patch Nov 10 '20 at 16:51