Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is memory profiling in ruby so hard?

Tags:

Or rather, why aren't there better tools for profiling memory in ruby, specifically rails apps?

Recently our rails app (hosted on heroku) has started seeing lots of R14 errors in the worker dynos. This means we're running out of memory. Bumping the dynos to 2x (512mb -> 1GB) only alleviated the problem temporarily, leading me to believe there is a memory leak somewhere. Naturally, my next step was to find a good profiling gem that can help me discover the source of the leak.

Maybe I'm just ignorant of the tools available, or maybe I just don't know how to use the ones I have. My wish is that I could install a gem and then run reports on the memory usage statistics. Hitting an endpoint to get a report is not really viable as my memory issues are isolated to worker dynos running delayed jobs.

I've looked at memprof, but it's 1.8 only.

I've looked at ruby-prof (awesome), but the memory profiling requires a patched ruby intepreter.

I've looked at GC::Profiler, but I don't understand how to find memory leaks with it.

So, is it just plain difficult to find memory leaks in ruby? Or am I missing the point somehow?

like image 262
Kevin Cantwell Avatar asked Apr 30 '13 19:04

Kevin Cantwell


People also ask

What causes Ruby memory bloat?

Slow Release. Another important cause of memory bloat in Ruby is a slow release of freed memory back to the system. In this situation, memory is freed much more slowly than the rate at which new memory blocks are allocated to objects.

How is memory managed in Ruby?

Memory in Ruby revolves around a certain set of factors, which as a whole manage the judicious use of system resources. Primarily, memory management relies on the Ruby runtime, the host operating system, and the system kernel.

What is memory leak in Ruby?

Memory leak happens when “memory which is no longer needed is not released”. In other words, running code loses control over allocated memory. Nothing leaks when the code of “Simple leak” is over or the variable runs out its scope of visibility.


1 Answers

Depending on your "type" of leak, You can run valgrind against ruby. Might require recompilation again though. In general it's hard because ruby does method allocation without firing any events, by default, so it's tricky to track. See also perftools.rb project, which somewhat works around this limitation.

like image 138
rogerdpack Avatar answered Sep 28 '22 01:09

rogerdpack