Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 + Heroku: Find out dyno's memory usage

Is there any way to find out how much memory a heroku web dyno is using? Let's say I wanna write a rake task that runs periodically and checks every dyno's memory usage. How could I do this? Thanks!

like image 949
sauronnikko Avatar asked Oct 15 '12 19:10

sauronnikko


1 Answers

Since Heroku runs on an Amazon instance with Linux, you can use the proc filesystem to get runtime system data. The /proc/<pid>/smaps file contains memory info on the running process and all the libraries that it loads. For the resident process size, do this:

f = File.open("/proc/#{Process.pid}/smaps")
f.gets # Throw away first line
l = f.gets # Contains a string like "Size:               2148 kB\n"
l =~ /(\d+) kB/  # match the size in kB 
f.close
$1.to_i  # returns matched size as integer

Update: The proc file system has an even better source, /proc/<pid>/status. There's an entry VmRSS: for the total resident memory portion and VmHWM: for the highest resident memory used (high water mark). For more details of these and other fields, see The Linux Kernel docs for the proc file system.

like image 176
Wolfram Arnold Avatar answered Nov 10 '22 17:11

Wolfram Arnold