1

Node.js アプリで大量のメモリ消費が発生しています。最大 100 MB の zip ファイルを次々にロードすると、「NodeBufferReader」としてメモリに保持されます。私が使用しているライブラリは JSZip と呼ばれ、https ://stuk.github.io/jszip/ にあります。

同じ zip ファイルに 2 回アクセスしても、メモリ使用量は増加しませんが、「余分な」.zip ファイルにアクセスするたびに、メモリは .zip ファイルのサイズ分だけ増加します。私がアクセスしているファイルはすべて約 100MB 以上であるため、すぐにかなり大きくなる可能性があることは想像に難くありません。

Node.js アプリケーションは、.zip ファイル内からファイルを読み取り、base64 データとして要求元に返す Websocket サーバーです。問題の関数は次のとおりです。

function handleFileRequest(args, connection_id) {
    var zipIndex = 0,
        pathLen = 0,
        zip_file = "",
        zip_subdir = "";
    try {
        if (args.custom.file.indexOf(".zip") > -1) {
            // We have a .zip directory!
            zipIndex = args.custom.file.indexOf(".zip") + 4;
            pathLen = args.custom.file.length;

            zip_file = args.custom.file.substring(0, zipIndex);
            zip_subdir = args.custom.file.substring(zipIndex + 1, pathLen);

            fs.readFile(zip_file, function (err, data) {
                if (!err) {
                    zipObj.load(data);
                    if (zipObj.file(zip_subdir)) {
                        var binary = zipObj.file(zip_subdir).asBinary();
                        var base64data = btoa(binary);
                        var extension = args.custom.file.split('.').pop();
                        var b64Header = "data:" + MIME[extension] + ";base64,";
                        var tag2 = args.custom.tag2 || "unset";
                        var tag3 = args.custom.tag3 || "unset";

                        var rargs = {
                            action: "getFile",
                            tag: args.tag,
                            dialogName: connections[connection_id].dialogName,
                            custom: {
                                file: b64Header + base64data,
                                tag2: tag2,
                                tag3: tag3
                            }
                        };
                        connections[connection_id].sendUTF(JSON.stringify(rargs));

                        rargs = null;
                        binary = null;
                        base64data = null;
                    } else {
                        serverLog(connection_id, "Requested file doesn't exist");
                    }
                } else {
                    serverLog(connection_id, "There was an error retrieving the zip file data");
                }
            });

        } else {
            // File isn't a .zip
        }
    } catch (e) {
        serverLog(connection_id, e);
    }
}

メモリの問題

この問題を解決するために、どんな助けも大歓迎です - ありがとう!

作業コードの例

function handleFileRequest(args, connection_id) {
    var zipIndex = 0,
        pathLen = 0,
        f = "",
        d = "";
    try {
        if (args.custom.file.indexOf(".zip") > -1) {
            // We have a .zip directory!
            zipIndex = args.custom.file.indexOf(".zip") + 4;
            pathLen = args.custom.file.length;

            f = args.custom.file.substring(0, zipIndex);
            d = args.custom.file.substring(zipIndex + 1, pathLen);

            fs.readFile(f, function (err, data) {
                var rargs = null,
                    binary = null,
                    base64data = null,
                    zipObj = null;

                if (!err) {

                    zipObj = new JSZip();
                    zipObj.load(data);

                    if (zipObj.file(d)) {
                        binary = zipObj.file(d).asBinary();
                        base64data = btoa(binary);
                        var extension = args.custom.file.split('.').pop();
                        var b64Header = "data:" + MIME[extension] + ";base64,";
                        var tag2 = args.custom.tag2 || "unset";
                        var tag3 = args.custom.tag3 || "unset";

                        rargs = {
                            action: "getFile",
                            tag: args.tag,
                            dialogName: connections[connection_id].dialogName,
                            custom: {
                                file: b64Header + base64data,
                                tag2: tag2,
                                tag3: tag3
                            }
                        };
                        connections[connection_id].sendUTF(JSON.stringify(rargs));
                    } else {
                        serverLog(connection_id, "Requested file doesn't exist");
                    }
                } else {
                    serverLog(connection_id, "There was an error retrieving the zip file data");
                }

                rargs = null;
                binary = null;
                base64data = null;
                zipObj = null;
            });

        } else {
            // Non-Zip file
        }
    } catch (e) {
        serverLog(connection_id, e);
    }
}
4

1 に答える 1