i am using sharp library to resize my host images before sending them to admin dashboard
code:
const sharp = require("sharp")
const path = require("path")
const fs = require("fs")
sharp.cache(false)
sharp.concurrency(1)
module.exports.media_get = async (req, res) => {
const { file } = req.params
const imagePath = path.join(GLOBALS.mediaPath, file)
if (fs.existsSync(imagePath)) {
const ext = imagePath.split(".").pop()
if (ext === "svg") {
res.sendFile(imagePath)
} else {
try {
const data = await sharp(imagePath)
.resize(200)
.toBuffer()
res.contentType(GLOBALS.mimeTypes[ext])
res.send(data)
} catch (err) {
console.error("eroor", err)
res.status(500).send("error")
}
}
}
}
But the problem is that after processing and sending 100 photos, RSS consumption increases and this amount of RAM is no longer freed
I tried using
sharp.cache(false)
sharp.concurrency(1)
to solve this problem, but it had no effect
It is safe to say that this problem does not occur in the RAM of my Windows on the local host, but when it runs on the host, the occupied RAM space is not freed (I use Windows 11 and the application that runs on the host is exactly the same as the application that runs on the local I do, they are the same)
my host is on ubuntu
What can I do so that this problem does not arise and the occupied RAM space is freed again?
I tried using
sharp.cache(false)
sharp.concurrency(1)
to solve this problem, but it had no effect
I tried to use jemalloc to solve this problem, but the problem was not solved, or maybe I installed and started this jemalloc wrongly.
We faced the same issue in production, hosted on heroku. We use sharp for processing image resize/convert/metadata. The ram usage would keep going up until we restarted the server. And it would keep going up again.
We solved it by jemalloc buildpack available for heroku (github repo). I recommend you to give jemalloc another shot.