title: Memory allocation failed - JavaScript heap out of memory
body: <p>I am facing a memory leak issue in my Node.js code. I am trying to stream read a CSV (sample file in link) file with 100k rows and process each entry in the file. The process struck after some time with a memory allocation error. </p>
  
  <blockquote>
    <p>"FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory"</p>
  </blockquote>
  
  <p><a href="https://docs.google.com/spreadsheets/d/1SJgTuHAEAV-JZeZvPwSK3NXrWZ_W-lyZj3y6qnOsW0Q/edit?usp=sharing" rel="nofollow noreferrer">sample csv:</a></p>
  
  <p><strong>My code sample</strong> </p>
  
  <pre><code>const fs = require('fs');
  const config = require('../config/config');
  const csv = require('csv-parser');
  const tls = require('../services/tls');
  
  processCSV('crawler', 'sample-csv.csv');
  </code></pre>
  
  <p><strong>Stream process csv file with 100k entries</strong> </p>
  
  <pre><code>async function processCSV (jobName, fileName) {
    return new Promise((resolve, reject) =&gt; {
      let filePath = config.api.basePath + fileName;
      fs.createReadStream(filePath)
          .on('error', () =&gt; {
            // handle error
            console.log('error processing csv');
            reject();
  
          })
          .pipe(csv())
          .on('data', (row) =&gt; {
            createJob(jobName, row);
          })
          .on('end', () =&gt; {
            // handle end of CSV
            console.log('Finished processing csv');
            resolve(filePath);
          })
    });
  }
  </code></pre>
  
  <p><strong>Validate each url in the csv file</strong></p>
  
  <pre><code>async function createJob (name, data) {
    let {hostname, port, ip} = data;
    let protocol = 'https';
    if (port === 80) {
      protocol = 'http';
    }
    let url = protocol + '://' + hostname;
    try {
      await tls.getHostData(url); // call an external api to get details of hostname
      return url;
    } catch (error) {
      return error;
    }
  }
  </code></pre>
  
  <p>I don't know which function is causing a memory leak. </p>