title: Issue With NodeJS Memory Consumption Statistics - Huge RSS & External Memory Usage
body: <p>While working on performance testing for a Node Project, I came across some memory reports which are a bit confusing to interpret in terms of performance and memory leaks.</p>
  
  <h2>Background</h2>
  
  <p>So I generated the reports using <a href="https://nodejs.org/api/process.html#process_process_memoryusage" rel="noreferrer">process.memoryUsage()</a> API that provides information about following -</p>
  
  <ul>
  <li><strong>RSS</strong> - subset of the total allocated memory including includes the heap, code segment and stack</li>
  <li><strong>heapTotal</strong> - memory allocated to the objects, strings, and closures</li>
  <li><strong>heapUsed</strong> - actual memory used by the objects, strings, and closures</li>
  <li><strong>external</strong> - memory usage of C++ objects bound to JavaScript objects managed by V8</li>
  </ul>
  
  <hr>
  
  <h2>The Problem</h2>
  
  <p>I recorded the memory snapshot after every request made to the server and plotted a graph for 100 requests -</p>
  
  <p><img src="https://i.stack.imgur.com/8zTwrl.png" width="900" height="500" /></p>
  
  <p><strong>The graph shows following information -</strong></p>
  
  <ol>
  <li>The <code>RSS</code> memory keeps increasing with every request served</li>
  <li>The <code>External Memory</code> usage is significant compared to <code>Heap Memory</code></li>
  <li>The <code>External Memory</code> goes down once the process is idle</li>
  <li>The <code>Total Heap</code> &amp; <code>Used Heap</code> memories are well under control</li>
  </ol>
  
  <hr>
  
  <h2>Findings</h2>
  
  <p><strong>As per my understanding, something outside Heap is causing the memory to rise.</strong></p>
  
  <p>I am using <code>sharp node package</code> which is resizing a lot of large images which ends up generating huge in memory buffers for those images.</p>
  
  <p><strong>In terms of memory, every request is serving 10 images that ends up generating 80 MB of buffer data</strong></p>
  
  <p><strong>This is where I came across <a href="https://github.com/nodejs/node/issues/21441" rel="noreferrer">this github issue</a> where impact of creating buffers in NodeJs is discussed</strong></p>
  
  <p><strong><a href="https://github.com/nodejs/node/issues/21441#issuecomment-399486693" rel="noreferrer">this answer</a> in the same github issue, explains how buffers can possibly occupy memory that <code>garbage collector</code> can't keep track of</strong></p>
  
  <hr>
  
  <h2>Questions</h2>
  
  <p><strong>Am I on the right track?</strong></p>
  
  <p><strong>If not, is using buffers in Node Applications not a proper way?</strong></p>