module Sys::Memory

The Memory module is a house for memory related singleton methods that don't require state.

Constants

HOST_VM_INFO64
HOST_VM_INFO64_COUNT
MEMINFO_REGEX
MEMORY_FILE
VERSION

The version of the sys-memory library.

Public Class Methods

free(extended: false) click to toggle source

The memory currently available, in bytes. By default this is only physical memory, but if the extended option is set to true, then free swap memory is also included.

# File lib/sys/linux/memory.rb, line 39
def free(extended: false)
  hash = memory
  extended ? (hash['MemFree'] + hash['SwapFree']) * 1024 : hash['MemFree'] * 1024
end
load(extended: false) click to toggle source

A number between 0 and 100 that specifies the approximate percentage of memory that is in use. If the extended option is set to true then swap memory is included in the calculation.

# File lib/sys/linux/memory.rb, line 57
def load(extended: false)
  (used(extended: extended) / total(extended: extended).to_f).round(2) * 100
end
memory() click to toggle source

Obtain detailed memory information about your host in the form of a hash. Note that the exact nature of this hash is largely dependent on your operating system.

# File lib/sys/linux/memory.rb, line 15
def memory
  hash = {}

  IO.foreach(MEMORY_FILE) do |line|
    key, value = MEMINFO_REGEX.match(line.chomp).captures
    hash[key] = value.to_i
  end

  hash
end
total(extended: false) click to toggle source

Total memory in bytes. By default this is only physical memory, but if the extended option is set to true, then swap memory is included as part of the total.

# File lib/sys/linux/memory.rb, line 30
def total(extended: false)
  hash = memory
  extended ? (hash['MemTotal'] + hash['SwapTotal']) * 1024 : hash['MemTotal'] * 1024
end
used(extended: false) click to toggle source

The memory, in bytes, currently in use. By default this is only physical memory, but if the extended option is set to true then swap is included in the calculation.

# File lib/sys/linux/memory.rb, line 48
def used(extended: false)
  hash = memory
  (total(extended: extended) - free(extended: extended) - hash['Buffers'] - hash['Cached'] - hash['Slab']) * 1024
end

Private Instance Methods

free(extended: false) click to toggle source

The memory currently available, in bytes. By default this is only physical memory, but if the extended option is set to true, then free swap memory is also included.

# File lib/sys/linux/memory.rb, line 39
def free(extended: false)
  hash = memory
  extended ? (hash['MemFree'] + hash['SwapFree']) * 1024 : hash['MemFree'] * 1024
end
load(extended: false) click to toggle source

A number between 0 and 100 that specifies the approximate percentage of memory that is in use. If the extended option is set to true then swap memory is included in the calculation.

# File lib/sys/linux/memory.rb, line 57
def load(extended: false)
  (used(extended: extended) / total(extended: extended).to_f).round(2) * 100
end
memory() click to toggle source

Obtain detailed memory information about your host in the form of a hash. Note that the exact nature of this hash is largely dependent on your operating system.

# File lib/sys/linux/memory.rb, line 15
def memory
  hash = {}

  IO.foreach(MEMORY_FILE) do |line|
    key, value = MEMINFO_REGEX.match(line.chomp).captures
    hash[key] = value.to_i
  end

  hash
end
total(extended: false) click to toggle source

Total memory in bytes. By default this is only physical memory, but if the extended option is set to true, then swap memory is included as part of the total.

# File lib/sys/linux/memory.rb, line 30
def total(extended: false)
  hash = memory
  extended ? (hash['MemTotal'] + hash['SwapTotal']) * 1024 : hash['MemTotal'] * 1024
end
used(extended: false) click to toggle source

The memory, in bytes, currently in use. By default this is only physical memory, but if the extended option is set to true then swap is included in the calculation.

# File lib/sys/linux/memory.rb, line 48
def used(extended: false)
  hash = memory
  (total(extended: extended) - free(extended: extended) - hash['Buffers'] - hash['Cached'] - hash['Slab']) * 1024
end