1

致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足

这种情况经常发生在我的Firebase 功能(出于绝望而提出4GB 内存配置),同时将一个大文件写入我的存储桶。

我对 Node 不是很好,但这是我的代码。我怀疑我只是在流中扮演一个假人。我可以使用帮助。

public sendToStorage(reportContents: any[], configuration: ISendToStorageConfiguration): Promise<string> {
    const destination = this._constructFileNameForReportJson(configuration);

    console.info("Storage destination: " + destination + ", " + this._bucket.name);

    return new Promise<string>(async (resolve, reject) => {
        const file = this._bucket.file(destination);

        const fileStream = file.createWriteStream({
            contentType : "application/json",
            public      : true,
            resumable   : false
        });

        // stream event handlers

        fileStream.on("finish", () => {
            const url = file.publicUrl();
            console.info("sendToStorage report contents written: " + url);

            resolve(url);
        });

        fileStream.on("error", error => {
            console.error(error);

            reject(error);
        });

        fileStream.write("[");

        for (let i = 0; i < reportContents.length; i++) {
            let writeBlock = JSON.stringify(reportContents[i]);

            writeBlock += i < reportContents.length - 1
                ? ",\n"
                : "\n";

            if (!fileStream.write(writeBlock))
                await new Promise(resolve => fileStream.once("drain", resolve));
        }

        fileStream.write("]");

        fileStream.end();
    });
}
4

0 回答 0