Class: BetterCap::Memory

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

Overview

This class is responsible for garbage collection and memory stats printing.

Instance Method Summary collapse

Constructor Details

#initializeMemory

Returns a new instance of Memory.


17
18
19
20
21
22
# File 'lib/bettercap/memory.rb', line 17

def initialize
  GC.enable
  s = GC.stat
  @total_allocs = s[:total_allocated_objects]
  @total_freed  = s[:total_freed_objects]
end

Instance Method Details

#optimize!Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bettercap/memory.rb', line 24

def optimize!
  GC.start
  begin
    s          = GC.stat
    new_allocs = s[:total_allocated_objects]
    new_freed  = s[:total_freed_objects]
    allocs_d   = nil
    freed_d    = nil

    if new_allocs < @total_allocs
      allocs_d = new_allocs.to_s.green
    elsif new_allocs > @total_allocs
      allocs_d = new_allocs.to_s.red
    else
      allocs_d = new_allocs
    end

    if new_freed < @total_freed
      freed_d = new_freed.to_s.red
    elsif new_freed > @total_freed
      freed_d = new_freed.to_s.green
    else
      freed_d = new_freed
    end

    Logger.debug "GC: allocd objects: #{allocs_d} freed objects: #{freed_d}"

    @total_allocs = new_allocs
    @total_freed  = new_freed
  rescue; end
end