Method: OS.rss_bytes

Defined in:
lib/os.rb

.rss_bytesObject

amount of memory the current process “is using”, in RAM (doesn't include any swap memory that it may be using, just that in actual RAM) raises 'unknown' on jruby currently


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/os.rb', line 126

def self.rss_bytes
  # attempt to do this in a jruby friendly way
  if OS::Underlying.windows?
    # MRI, Java, IronRuby, Cygwin
    if OS.java?
      # no win32ole on 1.5.x, so leave here for compatibility...maybe for awhile :P
      require 'java'
      mem_bean = java.lang.management.ManagementFactory.getMemoryMXBean
      mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used
    else
      wmi = nil
      begin
        require 'win32ole'
        wmi = WIN32OLE.connect("winmgmts://")
      rescue LoadError, NoMethodError => e # NoMethod for IronRuby currently [sigh]
        raise 'rss unknown for this platform ' + e.to_s
      end        
      processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}")
      memory_used = nil
      # only allow for one...
      for process in processes
        raise if memory_used
        memory_used = process.WorkingSetSize.to_i
        end
      memory_used
    end
  elsif OS.posix? # linux [though I've heard it works in OS X]
    kb = `ps -o rss= -p #{Process.pid}`.to_i # in kilobytes
  else
    raise 'unknown rss for this platform'
  end
end