Getting memory usage of my process from OSX using Ruby
Referring to this answer it seems like you need to call proc_pidinfo()
. I don't think there's a Ruby equivalent, so either you'll have to write a C-extension or use the ruby-ffi gem.
Other sources indicate Ruby 1.9.2 ships with a built in FFI -- but that version is not delivered with OS X.
Get current ruby process memory usage
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.
get cpu usage in a script in osx
sysctl
is easy to parse; you can get the load average by inspecting the vm.loadavg
value.
$ sysctl vm.loadavg
vm.loadavg: { 0.57 0.80 0.85 }
Ruby process memory structure
It is likely that your application is allocating objects that are then groomed by the Garbage Collector. You can check this with a call to GC.stat
Ruby does not release memory back to the operating system in any meaningful way. (if you're running MRI) Consequently, if you allocate 18GB of memory and 15GB gets garbage collected, you'll end up with your ~3GB of heap data.
The Ruby MRI GC is not a compacting garbage collector, so as long as there is any data in the heap the heap will not be released. This leads to memory fragmentation and the values that you see in your app.
Ruby processes on OSX keep going above 90%. What's up?
Have you tried opening a terminal window and running top
, or opening Activity Monitor
, then running each one individually and seeing when your CPU starts to climb? Both top
and Activity Monitor
will let you sort by the CPU load an app is generating. Use top -o cpu
and watch to see if the highest load is the app you ran or maybe something else the app is causing to run.
A Rails app will spike when it starts up but should settle down as it waits for incoming connections. If you have periodic tasks it's performing you should see those cause the CPU activity to spike again then drop when the task ends.
You're on a MacBook Pro. How much RAM do you have? Maybe your apps are running low and having to swap out too much? That would affect your overall system performance making the CPU work harder, causing it to heat up. A MacBook Pro's hard disk is designed for battery efficiency, not high performance, so if you're hitting the disk hard with a lot of database I/O, you could be heating the machine up and causing the apps to wait due to record locking or some sort of contention.
There's a lot of different things that can cause your machine and apps to slow down, and you haven't really given us a lot to work with, so those are some general ideas of what I'd look into.
rails console is maxing out Mac memory
I was experiencing the same problem for quite a while. I've eventually noticed that it's a problem with the listen
gem. Updating it is enough to fix it.
Update it to the latest version possible. If I remember correctly, it has to be at least v3.3
for the problem to go away.
Ruby: getting disk usage information on Linux from /proc (or some other way that is non-blocking and doesn't spawn a new process)
The information can be accessed via the system call statfs
http://man7.org/linux/man-pages/man2/statfs.2.html
I can see there is a ruby interface to this here:
http://ruby-doc.org/core-trunk/File/Statfs.html
How to fetch list of running processes on a system and sort them by various parameters
Check out sys-proctable:
require 'sys/proctable'
Sys::ProcTable.ps
To sort by starttime
:
Sys::ProcTable.ps.sort_by(&:starttime)
Related Topics
Ruby Replace String with Captured Regex Pattern
Library Not Loaded: /Usr/Local/Opt/Readline/Lib/Libreadline.6.Dylib (Loaderror)
Rails: How to Write Tests for a Ruby Module
What Is the Send() Method Used For
Profile a Rails Controller Action
Don't Create View Folder on Rails Generate Controller
Cannot Install Aptana Plugin on Eclipse 4.2
How to Make Rails 3 Assets Precompile Faster
How to Fire and Forget a Subprocess
Openssl, Rvm, Brew, Conflicting Error
Gem Dependencies Versions Meaning
Parsing Simple Xml with Nokogiri
How to Add a Method to the Global Scope in Ruby
What's the Difference Between Controllers and Actions in Ruby on Rails