0
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 > remaining.length)
      ? remaining.substring(lastLineFeed + 1, remaining.length)
      : "";

    for (var i = 0, length = lines.length; i < 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 > 0) {
        var account={
            username:name,
            password:remaining,
        };
         pass.test(account);
    };
  });

I tried to do something like test password of account "admin", pass.test 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?

4

1 に答える 1

0

私の限られた理解によると、実際には V8 エンジンによって課されていると思われる 1GB の制限を監視する必要があります。(ここにリンクがあります。実際には、現在の制限は 1.4 GB であり、これを手動で変更するために使用されるさまざまなパラメーターがリストされています。)ノードアプリをホストする場所に応じて、パラメーターを設定して、この制限を増やすことができます。ノード起動時のコマンドライン。繰り返しますが、これを行ういくつかの方法については、リンクされた記事を参照してください。

また、可能であれば、データ ストリームのようなもの (たとえば、DB などから) を配列などに変換する代わりに、バッファを使用することを確認することをお勧めします。これにより、データセット全体がメモリに読み込まれます。 . バッファ内にある限り、アプリの総メモリ フットプリントには影響しません。

実際には、意味がなく、アプリでは非常に非効率的であるように思われることの 1 つは、データの各チャンクを読み取るときに、これまでに蓄積したすべてのユーザー名に対してユーザーをチェックすることです。 LAST配列ではなく、あなたの配列配列。アプリがすべきことは、最後に読み込んだユーザー名とパスワードの組み合わせを追跡し、このユーザーの前のすべてのデータをremaining変数で削除して、メモリを節約することです。また、パスワード ファイルのすべての行を保持するすべてのリポジトリではないため、おそらく何かのように名前を変更する必要がありますbuffer。これは、forループ。パスワード ファイル内のデータをチャンクごとに読み取ることで、既に "ループ" しているためです。

于 2013-10-03T14:40:20.060 に答える