且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取当前的 ruby​​ 进程内存使用情况

更新时间:2022-04-12 02:32:30

一年前尝试解决这个问题的时候,我在网上做了大量的研究和 API 挖掘,结果只能通过系统调用 ps 来解决.

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.

在 OS X 10.7.2 和 Red Hat 4.1.2-13(在 EC2 上):

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.

稍加努力就可以清理,但大部分时间都花在调用 ps 并捕获其输出上,所以我认为不值得花时间.

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.