nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

Node.js process rss going down but heapTotal and heapUsed remain high #4201

Open ghost opened 1 year ago

ghost commented 1 year ago

Details

I am running an express server. We have a memory leak and I am trying to investigate it. It has been narrowed down to a specific function. To get an idea of how much memory the process is using I am using the default process.memoryUsage() provided by node. I am appending every 10 seconds to a file the formatted results of this method call. I am interested if after making N amount of calls to observe how the memory behaves, how long it takes for the garbage collector to clean up and so on. However, surprisingly I am seeing that the rss goes down but the heapTotal and heapUsed remain higher. I cannot find any other such cases. The behaviour remained in all of the runs I did.

I am attaching a snapshot of the findings:

11:56 and 12 seconds
{"rss":"260.79 MB","heapTotal":"154.4 MB","heapUsed":"139.69 MB","external":"2.62 MB","arrayBuffers":"1.64 MB"}

11:56 and 22 seconds
{"rss":"237.49 MB","heapTotal":"154.4 MB","heapUsed":"139.74 MB","external":"2.62 MB","arrayBuffers":"1.64 MB"}

11:56 and 32 seconds
{"rss":"54.63 MB","heapTotal":"154.4 MB","heapUsed":"139.79 MB","external":"2.62 MB","arrayBuffers":"1.64 MB"}

11:56 and 42 seconds
{"rss":"47.23 MB","heapTotal":"154.4 MB","heapUsed":"139.85 MB","external":"2.62 MB","arrayBuffers":"1.64 MB"}

Node.js version

v18.14.1

Example code

I cannot provide code since it is a large application.

Operating system

macOS

Scope

runtime

Module and version

Not applicable.

prettydiff commented 1 year ago

Although the rest of your process continues to consume a large amount of memory it does look like your RSS object is being garbage collected with a large amount of memory freed between 22 and 32 seconds.

ghost commented 1 year ago

Thank you for your response! I specifically posted this snippet to show how the RSS gets collected but the heapTotal and heapUsed remain stable. It is my understanding that the RSS should be always larger and that it includes the heap. I have not been able to find any material online where the RSS is smaller.

gireeshpunathil commented 1 year ago

It is my understanding that the RSS should be always larger and that it includes the heap

this is not necessarily true. RSS is the amount of actual memory paged into the process. When there is a memory demand from other processes, OS temporarily steals pages from your process (to get back later when it actually needs it). So RSS can be (and will be in many cases) lesser than the virtual memory. On the other hand, the virtual memory (heapTotal / heapUsed) represents the total allocated and total occupied memory respectively. (occupation does not imply active usage).

So I will deem no issues with your metrics. You will need to worry if and when the heapTotal keeps growing in response to running of your workload.

Hope this helps.

FridayCandour commented 8 months ago

what does heapTotal represent?

gireeshpunathil commented 8 months ago

what does heapTotal represent?

total Javascript heap size.

Murtatrxx commented 6 months ago

Hi, I’m experiencing this issue’s opposite (kinda), I see a gradual (day by day and few MBs) increase of process.rss but process.healTotal remains stable, does that implicitly mean a memory leak? Should I be worried? image

github-actions[bot] commented 2 weeks ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.