title: Memory Leak in server.js | React / Express / SSR
body: <p>I am running a react app that renders on a nodejs server. I saw after a few days that the memory of the nodeJs process increased depending on the number of visitors. Everytime I reloaded the page, the memory usage increased a bit.
  At the beginning the process takes about ~60MB memory. After a few days it increased to ~450MB. For now I am restarting the node-process to solve this problem.</p>
  
  <p>I think it is a problem with my React setup. Even if I only render a very small App I get the leak. Example:</p>
  
  <pre><code>// Express server
  app.get('*', async (req, res, next) =&gt; {
    try {
      const html = ReactDOM.renderToStaticMarkup(
        &lt;html&gt;
          &lt;head /&gt;
          &lt;body&gt;
            &lt;h1&gt;hello&lt;/h1&gt;
          &lt;/body&gt;
        &lt;/html&gt;,
      );
      res.status(200);
      res.send(`&lt;!doctype html&gt;${html}`);
    } catch (err) {
      next(err);
    }
  });
  </code></pre>
  
  <p>To check the memoryleak I am forcing garbage collection every 3 seconds and print out the memory usage while using <a href="https://github.com/mcollina/autocannon" rel="noreferrer">autocannon</a> to make a few thousand requests. This gives the following result:</p>
  
  <pre><code>Program is using 16234160 bytes of Heap. // Start, no requests yet
  Program is using 16177744 bytes of Heap.
  Program is using 16177864 bytes of Heap.
  Program is using 15185808 bytes of Heap. // Idle heap
  Program is using 15185808 bytes of Heap.
  Program is using 15185808 bytes of Heap.
  Program is using 15185808 bytes of Heap.
  Program is using 19199696 bytes of Heap. // Beginning of first 10k requests
  Program is using 20890376 bytes of Heap.
  Program is using 20201600 bytes of Heap. // New idle heap
  Program is using 20201600 bytes of Heap.
  Program is using 20201600 bytes of Heap.
  Program is using 20201600 bytes of Heap.
  Program is using 21761368 bytes of Heap. // Beginning of second 10k requests
  Program is using 23862168 bytes of Heap.
  Program is using 25191168 bytes of Heap.
  Program is using 24731176 bytes of Heap.
  Program is using 24512424 bytes of Heap. // New idle heap
  Program is using 24512424 bytes of Heap.
  Program is using 24512424 bytes of Heap.
  </code></pre>
  
  <p>The memory usage of course increases highly when I render the "real" React application. The following screenshot shows the difference between first time starting the app and <strong>one single request</strong> to the page:</p>
  
  <p><a href="https://i.stack.imgur.com/9WtAo.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9WtAo.png" alt="comparison"></a></p>
  
  <p>If I only return a string without React's <code>renderToStaticMarkup</code> the problem is gone (e.g. <code>res.send("&lt;!doctype html&gt;&lt;html&gt;&lt;head /&gt;&lt;body&gt;&lt;h1&gt;hello&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;");</code>.</p>
  
  <p>Am I missing something here? Or is there a known problem? Or is it maybe an express issue?</p>
  
  <p>I am running on node <code>v8.4.0</code> and here are my dependencies (I am using <a href="https://github.com/kriasoft/react-starter-kit" rel="noreferrer">react-starter-kit</a>):</p>
  
  <pre><code>"dependencies": {
      "@babel/polyfill": "^7.0.0-beta.44",
      "bluebird": "^3.5.1",
      "body-parser": "^1.18.2",
      "classnames": "^2.2.5",
      "cookie-parser": "^1.4.3",
      "core-js": "^2.5.4",
      "express": "^4.17.1",
      "express-http-proxy": "^1.1.0",
      "express-jwt": "^5.3.1",
      "history": "^4.7.2",
      "intl": "^1.2.5",
      "isomorphic-fetch": "^2.2.1",
      "isomorphic-style-loader": "^4.0.0",
      "node-fetch": "^2.1.2",
      "normalize.css": "^8.0.0",
      "pretty-error": "^2.1.1",
      "prop-types": "^15.6.1",
      "react": "^16.9.0",
      "react-dom": "^16.9.0",
      "react-helmet": "^5.2.0",
      "react-slick": "0.23.1",
      "serialize-javascript": "^1.4.0",
      "smoothscroll-polyfill": "^0.4.4",
      "source-map-support": "^0.5.4",
      "universal-router": "^6.0.0",
      "whatwg-fetch": "^2.0.4"
    },
  </code></pre>