Class: LinuxProcessMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/linux_process_memory.rb

Overview

This class will read the smap files for a process on a Linux system and report the memory usage for that process.

Constant Summary collapse

VERSION =
File.read(File.expand_path("../VERSION", __dir__)).strip.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid = Process.pid) ⇒ LinuxProcessMemory

Create a memory snapshot for the specified process.

Parameters:

  • pid (Integer) (defaults to: Process.pid)

    The process ID to snapshot. Defaults to the current process.



39
40
41
42
# File 'lib/linux_process_memory.rb', line 39

def initialize(pid = Process.pid)
  @pid = pid
  @stats = (self.class.supported? ? read_smaps : Hash.new(-1))
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



25
26
27
# File 'lib/linux_process_memory.rb', line 25

def pid
  @pid
end

Class Method Details

.supported?Boolean

Returns true if the current platform is Linux.

Returns:

  • (Boolean)


31
32
33
# File 'lib/linux_process_memory.rb', line 31

def supported?
  RUBY_PLATFORM.match?(LINUX_MATCHER)
end

Instance Method Details

#pss(units = :bytes) ⇒ Numberic Also known as: proportional

Returns the proportional set size for the process.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


72
73
74
# File 'lib/linux_process_memory.rb', line 72

def pss(units = :bytes)
  convert_units(@stats[:Pss], units)
end

#referenced(units = :bytes) ⇒ Numberic

Returns the referenced memory size for the process (i.e. memory that is actively being used) that cannot be reclaimed.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


117
118
119
# File 'lib/linux_process_memory.rb', line 117

def referenced(units = :bytes)
  convert_units(@stats[:Referenced], units)
end

#rss(units = :bytes) ⇒ Numberic Also known as: resident

Returns the resident set size for the process.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


60
61
62
# File 'lib/linux_process_memory.rb', line 60

def rss(units = :bytes)
  convert_units(@stats[:Rss], units)
end

#shared(units = :bytes) ⇒ Numberic

Returns the shared memory used by the process.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


106
107
108
# File 'lib/linux_process_memory.rb', line 106

def shared(units = :bytes)
  convert_units(@stats[:Shared_Clean] + @stats[:Shared_Dirty], units)
end

#swap(units = :bytes) ⇒ Numberic

Returns the swap used by the process.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


96
97
98
# File 'lib/linux_process_memory.rb', line 96

def swap(units = :bytes)
  convert_units(@stats[:Swap], units)
end

#total(units = :bytes) ⇒ Numberic

Returns the total memory usage for the process.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


50
51
52
# File 'lib/linux_process_memory.rb', line 50

def total(units = :bytes)
  convert_units(@stats[:Rss] + @stats[:Swap], units)
end

#uss(units = :bytes) ⇒ Numberic Also known as: unique

Returns the uniq set size for the process.

Parameters:

  • units (Symbol) (defaults to: :bytes)

    The units to return the memory usage in. Valid values are :bytes, :kilobytes, :megabytes, :gigabytes, :kb, :mb, :gb, :k, :m, :g. Defaults to :bytes.

Returns:

  • (Numberic)


84
85
86
# File 'lib/linux_process_memory.rb', line 84

def uss(units = :bytes)
  convert_units(@stats[:Private_Clean] + @stats[:Private_Dirty], units)
end