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!
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.
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