title: How should I avoid out of memory using nodejs?
body: <pre><code>var  pass = require('./pass.js');
  var  fs   = require('fs');
  var path  = "password.txt";
  var name  ="admin";
  var
    remaining = "",
    lineFeed = "\r\n",
    lineNr = 0;
  
  var log = 
    fs.createReadStream(path, { encoding: 'utf-8' })
    .on('data', function (chunk) {
      // store the actual chunk into the remaining
      remaining = remaining.concat(chunk);
  
      // look that we have a linefeed
      var lastLineFeed = remaining.lastIndexOf(lineFeed);
  
      // if we don't have any we can continue the reading
      if (lastLineFeed === -1) return;
  
      var
        current = remaining.substring(0, lastLineFeed),
        lines = current.split(lineFeed);
  
      // store from the last linefeed or empty it out
      remaining = (lastLineFeed &gt; remaining.length)
        ? remaining.substring(lastLineFeed + 1, remaining.length)
        : "";
  
      for (var i = 0, length = lines.length; i &lt; length; i++) {
        // process the actual line
        var account={
          username:name,
          password:lines[i],
        };
        pass.test(account);
      }
    })
    .on('end', function (close) {
      // TODO I'm not sure this is needed, it depends on your data
      // process the reamining data if needed
      if (remaining.length &gt; 0) {
          var account={
              username:name,
              password:remaining,
          };
           pass.test(account);
      };
    });
  </code></pre>
  
  <p>I tried to do something like test password of account "admin", <em>pass.test</em> is a function to test the password, I download a weak password dictionary with a large number of lines,so I search for way to read that many lines of weak password,but with code above, the lines array became too large ,and run out of memory,what should I do?</p>