-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If you're having memory leaks/bloating #1041
Comments
Other way is use jemalloc by setting LD_PRELOAD environment variable. Debian example: After this change, total memory usage has dropped from "out off memory" to less than 200M per server instance. |
Please also see #955 for more evidence that the choice of memory allocator is important. |
If someone has problems with using libjemalloc1 on debian 10 (buster), its now version 2. Eg. |
I can confirm that using jemalloc fixes the memory issue. I set jemmaloc as system wide memory allocator, and the memory usage has dropped immensely and my app is no longer crashing because of memory not releasing. Thank you for all the suggested ideas and solutions. If anyone is wondering, this stackoverflow thread helped me with the installation and usage of jemalloc. |
@shwao Thank you, updating the environment variable fixed it for me. Context: I updated to version 2 since version one is not found, but didn't update the env variable, updating it fixed it for me. For anyone using docker here is the memory allocator change code:
|
Necro'ing this thread to say that the jemalloc2 fix is still applicable in February of 2023, in the context of an unexplained memory leak of a NextJS app on an Ubuntu production server. Subject to unexpected memory spikes over the next few days, our memory usage has gone from ~500 MB -- which would climb rapidly with reloading of images and never come back down -- to a steady baseline of ~300 MB with only minor blips on repeated reloads. This is a particularly wicked leak to track for the inexperienced, as the production-only nature of Next's use of sharp means that debugging the local Node server will (definitionally) not catch it. Consider this comment something like bug-hunting SEO! |
On an SEO note, for those visiting this page who haven't yet found the docs, please see https://sharp.pixelplumbing.com/install#linux-memory-allocator When using Linux, sharp v0.28.0+ will attempt to detect the memory allocator and adjust its concurrency accordingly to help reduce fragmentation, so please ensure you're using a recent version. |
Try running your sharp image processing in a child_process spawn. I've seen lots of posts here that generally conclude it's something node-related with RSS memory. I can't say one way or another if this is the case, and I've generally seen multiple suggestions to fix it in my search for a solution, some seem to work, others don't - this worked for me.
In my case running sharp in a server/express is where I saw the bloat. I'm using multer to upload image files and then attempting to compress/reduce them on the fly. Since multer stores the images and spits out an array of file locations, I just passed the array into my child process as a JSON string param.
...My image processing module spawned in the child process...
...The image.js module where the sharp code and promise chain is constructed. Pardon the many files, this all needs refactoring.
Hope this concept helps people who are stuck on memory problems. Sharp is amazing, lets keep using it!
The text was updated successfully, but these errors were encountered: