Search code examples
node.jsmemory-leaksgarbage-collectionv8dynatrace

NodeJS v14 memory issues: Resident Set grows a lot


After I upgraded a NodeJS server application from v6 to v14, I am facing with a strange memory behavior.

I noticed this issue (if it is) when I analyzed the memory usage using Dynatrace:

  1. the process gives no problems, nor restarts nor failure have been encountered: all is working fine

  2. the total memory usage increases constantly until it reaches about 5GB (we used --max-memory-restart 5G option when to run the application — who knows if it has something to do with it ...)

  3. the Heap size has its usual value and remains stable around 100~150MB

  4. the Resident Set grows constantly until it reaches 5GB and never broken this cap limit (furthermore: the restart security option features has been never triggered)

  5. I encounter this "issue" only in NodeJS v14, not in NodeJS v6, the application base-code is the same

Some details about memory usage here:

enter image description here

enter image description here

I haven't a great knowledge about JS Engine V8 memory structure and usage, but I given a read to following lectures:

Despite these great lectures, I am not convinced at all what may causes this Resident Set memory growing int this way. Since it's not causing me any real problems, can I gloss over? Or should I deepen, if so, what do you advise me to do?


Solution

  • Within the v8 memory chart, Dynatrace does not only show the heap memory, it also compares it with the resident set size of the whole process. Some may find this confusing, but this helps tremendously to separate JS memory leaks from native memory leaks. In your case there seems to be a potential native memory leak that is caused by native modules.

    You mentioned the --max-memory-restart parameter, so I guess you are using PM2. It's official documentation is not clear about whether the tool monitors just the memory used on the heap or overall memory usage. According to this article it seems that PM2 is not able to cover also native memory. Even more interesting are this bug report (closed but according to latest comments still not fixed) and the accepted answer to this question on Stack Overflow. It seems that even your version of PM2 may contain native memory leaks.

    Here you can find more information on how to analyze a native memory leak in NodeJS. The accepted answer of the mentioned question from above lists also some links to alterntives for PM2.

    By the way, Dynatrace will tell you if the increasing memory usage should have any measurable negtive effect on your services / applications or on end user experience.