zombocom/derailed_benchmarks

Inconsistent results while measuring memory used at require time

Closed this issue · 1 comments

Trying to measure the memory induced by a gem using the following command.
$ bundle exec derailed bundle:mem

  1. The values of memory used by individual gems and 'TOP' are different in different machines. The difference is more than 7 MiB. Why?
  2. Is there a recommended way to measure the memory induced by a gem?

The values of memory used by individual gems and 'TOP' are different in different machines. The difference is more than 7 MiB. Why?

Measuring memory is actually quite complicated. There are several layers of indirection between the OS and the physical memory. There are several ways to report and measure memory (PSS, RSS, Swap) and because Ruby uses GC and tries to minimize allocation calls (to malloc) a small deviation or variation can cause sometimes large memory swings (due to the growth factor).

In short, that call doesn't just measure memory of a single gem, it measures it in context of a whole Gemfile where many other libraries are also being required.

I would focus less on the absolute numbers and more on the relative ones. The command can be used to identify problems with large gems.

Is there a recommended way to measure the memory induced by a gem?

You're looking at it. If you want more fine-grained data then I recommend using memory_profiler you can use it to wrap a require call. Though note due to the "growth factor" issue I mentioned earlier, the amount of memory a gem causes your app to use may be different than the amount of memory it actually needs. (if for example, it pushes your app over a limit and triggers a major GC + an allocation).

I hope that helps