How to Find a Memory Leak on Heroku

How can I find a memory leak on Heroku?

The GC should do the clean up, and probably does.

You can force the GC with GC.start; if many objects were not collected this will, but I suspect that is not the issue.

Is it possible you somehow create a bunch of objects and never release them, by keeping cached copies or something?

I'm unfamiliar with the existing tools to check this, but you may want to check which objects exist using ObjectSpace. For example:

ObjectSpace.each_object.with_object(Hash.new(0)){|obj, h| h[obj.class] +=1 }
# => a Hash with the number of objects by class

If you get an unexpected number for one of your classes, for instance, you would have a better idea of where to look for.

Objects memory leaks (Heroku to 1.5 GB memory usage when nodejs is configured to max 512M)

You can use node-memwatch package to monitor for the memory leak and when there is a memory leak you can restart the app from the code itself.

Restart a node.js app from code level

Rails: Troubleshooting a memory leak on Heroku ( perhaps Nokogiri)

Parsing a large xml file and turning it into a document tree will in general create an in memory representation that is far larger that the xml data itself. Consider for example

<foo attr="b" />

which is only 16 bytes long (assuming a single byte character encoding). The in memory representation of this document will include an object to represent the element itself, probably an (empty) collection of children, a collection of attributes for that element containing at least one thing. The element itself has properties likes its name, namespace pointers to its parent document and so on. The data structures for each of those things is probably going to be over 16 bytes, even before they're wrapped in ruby objects by nokogiri (each of which has a memory footprint which is almost certainly >= 16 bytes).

If you're parsing large xml files you almost certainly want to use a event driven parser like a SAX parser that responds to elements as they are encountered in the document rather than building a tree representation on the entire document an then working on that.

Heroku connect.sessions() memory leak, not scaled

If you don't explicitly configure a session store, connect.session() (which you should probably replace by express-session, because the latter is still being maintained while the former was last updated 4 years ago) uses an in-memory object to store sessions.

Not only will this gradually increase your app's memory usage (which is what the warning is about), it also means that all your sesssions will be gone if your server has to restart.

To fix this, you should configure a persistent session store, where session data will be stored in a more persistent manner, usually in a database. A list of session stores can be found here.

Since you're already using PostgreSQL, connect-pg-simple seems like a logical candidate.

Sidekiq memory leak on Heroku

It was one of the gems in the :production group. I was using it for profiling, and looks like it did a poor job of it. I've replaced it with a similar gem and now there's no memory leak.



Related Topics



Leave a reply



Submit