Get current ruby process memory usage

前端 未结 6 989
囚心锁ツ
囚心锁ツ 2020-12-07 17:36

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 thi

6条回答
  •  天命终不由人
    2020-12-07 18:17

    When trying to solve this problem a year ago, I did a lot of online research and API digging and was only able to solve it via a system call to ps.

    In both OS X 10.7.2 and Red Hat 4.1.2-13 (on EC2):

    pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)
    

    This fetches and places the resident memory size of the process in kilobytes into the size variable.

    With a little effort this could be cleaned up, but most of the time is spend calling ps and capturing its output, so I don't think it is worth the time.

提交回复
热议问题