Last active
December 12, 2023 04:29
-
-
Save mtsmfm/94f8ff2c7216061b82aed9064a72861d to your computer and use it in GitHub Desktop.
NextJS memory issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createServer } from "http"; | |
import { Writable } from "node:stream"; | |
import { createWriteStream } from "node:fs"; | |
const prettyMemoryUsage = (mem) => { | |
const formattedMemory = Object.entries(mem).map(([key, value]) => { | |
const formattedValue = (value / 1024 / 1024).toFixed(2); | |
return `${key}: ${formattedValue} MB`; | |
}); | |
return formattedMemory.join("\n"); | |
}; | |
createServer(async (req, res) => { | |
const memHistory = []; | |
const timer = setInterval(() => { | |
memHistory.push(process.memoryUsage()); | |
}, 0); | |
const outStream = createWriteStream("/dev/null"); | |
if (req.url === "/dup") { | |
const dupReq = new Request("http://localhost/foo", { | |
body: req, | |
method: req.method, | |
headers: req.headers, | |
duplex: "half", | |
}); | |
await dupReq.body.pipeTo(Writable.toWeb(outStream)); | |
} else { | |
req.pipe(outStream); | |
await new Promise((resolve) => { | |
outStream.on("finish", () => { | |
resolve(undefined); | |
}); | |
}); | |
} | |
clearInterval(timer); | |
const result = prettyMemoryUsage( | |
memHistory.reduce((acc, mem) => | |
Object.entries(acc).reduce( | |
(accc, [key, value]) => ({ | |
...accc, | |
[key]: Math.max(value, mem[key]), | |
}), | |
{}, | |
), | |
), | |
); | |
res.statusCode = 200; | |
res.setHeader("Content-Type", "text/plain"); | |
res.end(result); | |
process.exit(); | |
}).listen(3000); | |
console.log("ready"); | |
/* | |
$ fallocate -l 1G example | |
$ node foo.mjs &; sleep 2; curl localhost:3000 -X POST -F 'file=@./example' | |
[1] 4385 | |
ready | |
rss: 82.96 MB | |
heapTotal: 8.89 MB | |
heapUsed: 5.19 MB | |
external: 34.07 MB | |
arrayBuffers: 32.01 MB% [1] + 4385 done node foo.mjs | |
$ node foo.mjs &; sleep 2; curl localhost:3000/dup -X POST -F 'file=@./example' | |
[1] 4478 | |
ready | |
rss: 1085.23 MB | |
heapTotal: 49.47 MB | |
heapUsed: 30.50 MB | |
external: 996.09 MB | |
arrayBuffers: 992.52 MB | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment