Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current ruby process memory usage

I'd like to format my Logger output to include the current memory usage, for part of a long-running process.

Is there anything built-in to Ruby for this, a bit like PHP's memory_get_usage()? Or do I have to execute some shell commands to get it from ps?

like image 516
d11wtq Avatar asked Aug 28 '11 12:08

d11wtq


People also ask

How to check memory usage in Ruby?

The Memory Profiler gem is an effective way of tracking the memory usage of ruby code. This command will send a bunch of requests to your application, and track memory usage over time. In case of a memory leak, the memory usage will keep on increasing over time.

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.

What is RSS memory in Linux?

In computing, resident set size (RSS) is the portion of memory occupied by a process that is held in main memory (RAM). The rest of the occupied memory exists in the swap space or file system, either because some parts of the occupied memory were paged out, or because some parts of the executable were never loaded.


1 Answers

The NewRelic gem provides simple RSS usage implementations for a number of operating systems and ruby runtimes with their MemorySampler class.

Include the newrelic_rpm gem in your Gemfile and invoke it thus:

NewRelic::Agent::Samplers::MemorySampler.new.sampler.get_sample 

and it returns the number of megabytes of memory the current process holds as the RSS.

The implementation prefers in-process counters where available (jruby), use the /proc/#{$$}/status on Linux, and fall back to ps everywhere else.

like image 130
rud Avatar answered Oct 19 '22 21:10

rud