Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js web server on heroku - constant memory grow

My app is up and running on heroku on 5 parallel dynos. There is constant load of 500-1500 req/min so up to 25 req/sec. The problem is that RSS memory is constantly growing. For now I am manually restarting app when memory reaches dangerous level (maximum memory used by 1 heroku dyno is 512mb). Memory chart looks like this (upper gray axis is at 512mb limit):

enter image description here

Moments (on chart) when memory is released are when I am restarting app.

The strange thing is that it happens only when there is constant load on server. When there is for example 2 minutes load on server then memory increases and after that in goes down again. So it seems that for some reason garbage collector is not working properly (it is no collecting the garbage until server load is finished and app is not busy).

Is there anything I can do about it? It is not memory leak I think because memory is released when there is no load on a server...

What I tried so far was:

  1. Call GC manually in 2 minute intervals
  2. Setting --max-old-space-size=300 however nothing happens when memory hits 300mb - it is still increasing.

Maybe there are other options that could help?

Node version is 0.10.20


I used node memwatch package and I managed to gather heap diffs from app:

  • http://jsonfiddle.net/auf8d (heap diff between 11 minutes, +37 MB)
  • http://jsonfiddle.net/4nvcd (heap diff between 15 minutes, +50 MB)

So there must be kind of leak I guess. The biggest memory changes from first diff:

        ...
        {
          "what": "Array",
          "size_bytes": 9320312,
          "size": "8.89 mb",
          "+": 79086,
          "-": 10215
        },
        ...
        {
          "what": "Closure",
          "size_bytes": 2638224,
          "size": "2.52 mb",
          "+": 36826,
          "-": 184
        },
        {
          "what": "Native",
          "size_bytes": 21471232,
          "size": "20.48 mb",
          "+": 546,
          "-": 0
        },
        {
          "what": "String",
          "size_bytes": 2068264,
          "size": "1.97 mb",
          "+": 36968,
          "-": 1223
        },
        ...

What is Native object (it allocated 20mb mem!)? Could you give me advice on how to investigate what exactly is causing the leak?

like image 627
user606521 Avatar asked Sep 28 '14 23:09

user606521


People also ask

How to optimize Heroku Dyno memory for Node JS applications?

This post goes over how to optimize Heroku dyno memory for Node.js applications. Then you need to check your app for memory leaks or upgrade to a larger dyno size. If your dyno has more than 512 MB RAM, then you’re not making use of all the available memory (depending on your Node.js version, the default memory limit can be 512 MB).

How much memory does a Heroku server use?

Heroku memory limits free, hobby, and standard-1X dynos can use 512 MB of memory, while standard-2X dynos have access to 1 GB. performance-M and performance-L dynos provide 2.5 and 14 GB of memory. Memory use over these limits writes to disk at much slower speeds than direct RAM access.

How does Heroku work with npm dependencies?

When subsequent dependencies are added, npm will make changes to this file, so be sure to add those changes to git too. When an app is deployed, Heroku reads the package.jsonto install the appropriate node version and the package-lock.jsonto install the dependencies. Run the app locally

Why can’t I deploy a static web server on Heroku?

The problem is that Heroku doesn’t offer this functionality out of the box, instead it lets you deploy apps in various languages (Ruby, Node.js, Python, Java, and PHP). I’m gonna guide you through how to create a simple static web server in node.js deployed on Heroku!


1 Answers

The problem was fixed by simply not using postgres native bindings (https://github.com/brianc/node-postgres#native-bindings). When I stopped using native bindings (I am using Sequelize.js so I just changed native flag to false) memory started to behave normally...

It seems that there might be leak in pg native bindings?

like image 86
user606521 Avatar answered Oct 31 '22 16:10

user606521