FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory when processing large files with fs

17,689

Solution 1

I have finally found the solution to the problem ! I needed to launch the process adding the --max-old-space-size=8192 param to node process like so:

node --max-old-space-size=8192 ./myScript.js

I processed my 1.3GB without a problem !!

Solution 2

You can increase the amount of memory allocated to the command by running the following command prior to running Snyk:

Linux/macOS

export NODE_OPTIONS=--max-old-space-size=8192 For example:

export NODE_OPTIONS=--max-old-space-size=8192 snyk test export NODE_OPTIONS=--max-old-space-size=8192 snyk monitor

Windows

From the control panel go to System -> Advanced system settings -> Environment Variables -> New (user or system)

enter image description here

Share:
17,689
TOPKAT
Author by

TOPKAT

Love to play music, draw and be with my family. Coding 90% of my time. I work as a lead developer in R&D dept. in a crypto / neo bank that's name akt.io. Here is my personal professional website https://nicewebagence.com A graphical novel I am working on https://dragonnoir.io My github: https://github.com/top-kat

Updated on June 05, 2022

Comments

  • TOPKAT
    TOPKAT about 2 years

    I have a nodeJs script that process a bunch of large .csv files (1.3GB for all). It run for a moment and throw this error:

    FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

    I have tried to put #!/usr/bin/env node --max-old-space-size=4096 at the beginning of my file but this didn't solve my problem...

    Tried to google it but nearly no pertinent info about my issue..

    Can I flush memory once I do not need file content ? Do I need to allocate more memory to nodeJs ?

    Thanks ;)

    Here is my code sample:

    fs.readdir(dirName, function(err, filenames) {
        if (err) console.error(err);
        else {
            filenames.forEach(function(filename) {
                fs.readFile(dirName + filename, 'utf-8', function(err, content) {
                    if (err) console.error(err);
                    else processFile(content);
                });
            });
        }
    });