3

I have a memory leak in my express application. The RSS memory starts at around 80Mb and grows slowly over time to reach around 2-3Gb over a couple of days. I have tried using the --inspect flag to start the process and debug in chrome inspector but not able to find out the exact issue. The Object allocation over a period of time is not that high in this case. The highest memory consumption is by strings as one can see in the attached screenshot. Can anyone point me in the right direction to fix this leak?enter image description here

I have also attached the RSS graph. The decline in memory is due to shutting down the server. enter image description here

Platform: Linux - Ubuntu 18.04.5

  • Any chance you can share your code, e.g. through github? – eol Dec 23 '20 at 07:32
  • @eol Unfortunately, I cannot share the source code. It's a production app. – Aditya Varma Dec 23 '20 at 10:59
  • It seems like your JavaScript process does not cause the leak (If the first picture actually shows a before and after screenshot). Do you print something in the console? Since when having the developer tools open, all console output will be saved in memory. This could be a potential cause. Closing the developer tools should free all memory. – WolverinDEV Dec 23 '20 at 18:12
  • @WolverinDEV There is surely a lot of logging done but I suspect that has anything to do with the memory leak. The heapdump doesn't change much over a period of time. The RSS grows significantly. I read a few articles and found out that this is more of memory bloat than a memory leak. – Aditya Varma Dec 24 '20 at 04:07
  • You could try to analyze the leak using https://github.com/v8/sampling-heap-profiler -> it allows you to see the actual stack that allocated this huge string. – eol Dec 24 '20 at 09:01
  • @eol It doesn't work on the latest node. https://github.com/v8/sampling-heap-profiler/issues/58 – Aditya Varma Dec 24 '20 at 10:01
  • 1
    @AdityaVarma Well I did either but it actually turned out in my app to be the issue (I had the exact same behaviour). You could try to disable logging and take a closer look. A significant increase in RSS could be memory bloat/fragmentation but out of experience JS is pretty awesome aware that. Are you using any native modules? – WolverinDEV Dec 25 '20 at 17:49
  • @WolverinDEV I am using some native modules. I ran the following command to find out which native modules are being used: `find node_modules -type f -name "*.node" 2>/dev/null | grep -v "obj\.target"` Native modules are: `iconv.node node_expat.node genx.node sharp.node fsevents.node gcstats.node unix_dgram.node` – Aditya Varma Jan 06 '21 at 09:18

1 Answers1

1

Well, it was a native module sharp leaking memory. It's being used in the application for image processing. We need to run the application with libjemalloc which works better with freeing the unused memory back to the OS. We can set the memory allocator by using the following command:

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1