Module: CzSystemInfo::Memory

Defined in:
lib/cz_system_info/memory.rb

Overview

The Memory module provides various functions that return information regarding the memory on your system.

Class Method Summary collapse

Class Method Details

.free(extended: false) ⇒ Object

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.



42
43
44
45
# File 'lib/cz_system_info/memory.rb', line 42

def free(extended: false)
  hash = memory
  extended ? (hash['MemFree'] + hash['SwapFree']) * 1024 : hash['MemFree'] * 1024
end

.load(extended: false) ⇒ Object

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.



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

def load(extended: false)
  (used(extended: extended) / total(extended: extended).to_f).round(2) * 100
end

.memoryObject

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.



18
19
20
21
22
23
24
25
26
27
# File 'lib/cz_system_info/memory.rb', line 18

def memory
  hash = {}

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

  hash
end

.total(extended: false) ⇒ Object

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.



33
34
35
36
# File 'lib/cz_system_info/memory.rb', line 33

def total(extended: false)
  hash = memory
  extended ? (hash['MemTotal'] + hash['SwapTotal']) * 1024 : hash['MemTotal'] * 1024
end

.used(extended: false) ⇒ Object

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.



51
52
53
54
# File 'lib/cz_system_info/memory.rb', line 51

def used(extended: false)
  hash = memory
  (total(extended: extended) - free(extended: extended) - hash['Buffers'] - hash['Cached'] - hash['Slab']) * 1024
end