Module: Sys::Memory

Extended by:
FFI::Library
Defined in:
lib/sys/osx/memory.rb,
lib/sys/version.rb,
lib/sys/linux/memory.rb,
lib/sys/windows/memory.rb

Overview

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

Defined Under Namespace

Classes: MemoryStatusEx, PerformanceInformation

Constant Summary collapse

VERSION =

The version of the sys-memory library.

'0.1.0'

Class Method Summary collapse

Class Method Details

.free(extended: false) ⇒ Object

The physical memory currently available, in bytes. This is the amount of physical memory that can be immediately reused without having to write its contents to disk first.

If the extended option is set to true then available swap (pagefile) memory is included as part of the total.



143
144
145
146
# File 'lib/sys/osx/memory.rb', line 143

def free(extended: false)
  hash = memory
  extended ? hash[:free] + hash[:swap_available] : hash[:free]
end

.load(extended: false) ⇒ Object

A number between 0 and 100 that specifies the approximate percentage of physical memory that is in use.

If the extended option is set to true then swap memory is included in the calculation.



160
161
162
# File 'lib/sys/osx/memory.rb', line 160

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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sys/osx/memory.rb', line 75

def memory
  hash = {}
  page_size = 4096 # Possibly changed later

  begin
    optr = FFI::MemoryPointer.new(:uint64_t)
    size = FFI::MemoryPointer.new(:size_t)
    size.write_int(optr.size)

    if sysctlbyname('hw.memsize', optr, size, nil, 0) < 0
      raise SystemCallError.new('sysctlbyname', FFI.errno)
    end

    hash[:total] = optr.read_uint64
  ensure
    optr.free if optr && !optr.null?
    size.clear
  end

  begin
    swap = Swap.new
    size.write_int(swap.size)

    if sysctlbyname('vm.swapusage', swap, size, nil, 0) < 0
      raise SystemCallError.new('sysctlbyname', FFI.errno)
    end

    hash[:swap_total] = swap[:xsu_total]
    hash[:swap_available] = swap[:xsu_avail]
    hash[:swap_used] = swap[:xsu_used]
    page_size = swap[:xsu_pagesize]
  ensure
    size.free if size && !size.null?
  end

  host_self = mach_host_self()
  vmstat = VmStat.new
  count = FFI::MemoryPointer.new(:size_t)
  count.write_int(vmstat.size)

  rv = host_statistics64(host_self, HOST_VM_INFO64, vmstat, count)
  raise SystemCallError.new('host_statistics64', rv) if rv != 0

  hash[:free] = vmstat[:free_count] * page_size
  hash[:active] = vmstat[:active_count] * page_size
  hash[:inactive] = vmstat[:inactive_count] * page_size
  hash[:speculative] = vmstat[:speculative_count] * page_size
  hash[:wire] = vmstat[:wire_count] * page_size
  hash[:compressed] = vmstat[:compressor_page_count] * page_size

  hash
ensure
  count.free if count && !count.null?
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 (pagefile) memory is included as part of the total.



134
135
136
137
# File 'lib/sys/osx/memory.rb', line 134

def total(extended: false)
  hash = memory
  extended ? hash[:total] + hash[:swap_total] : hash[:total]
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 (pagefile) is included in the calculation.



152
153
154
# File 'lib/sys/osx/memory.rb', line 152

def used(extended: false)
  total(extended: extended) - free(extended: extended)
end