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
?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With