Class: Autotuner::Heuristic::GCCompact

Inherits:
Base
  • Object
show all
Defined in:
lib/autotuner/heuristic/gc_compact.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

disable!, enabled?

Constructor Details

#initialize(_system_context) ⇒ GCCompact

Returns a new instance of GCCompact.



12
13
14
15
16
# File 'lib/autotuner/heuristic/gc_compact.rb', line 12

def initialize(_system_context)
  super

  @called_gc_compact = nil
end

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/autotuner/heuristic/gc_compact.rb', line 7

def supported?
  true
end

Instance Method Details

#call(request_context) ⇒ Object



22
23
24
25
26
# File 'lib/autotuner/heuristic/gc_compact.rb', line 22

def call(request_context)
  return unless @called_gc_compact.nil?

  @called_gc_compact = request_context.before_gc_context.stat[:compact_count] > 0
end

#debug_stateObject



63
64
65
66
67
# File 'lib/autotuner/heuristic/gc_compact.rb', line 63

def debug_state
  {
    called_gc_compact: @called_gc_compact,
  }
end

#nameObject



18
19
20
# File 'lib/autotuner/heuristic/gc_compact.rb', line 18

def name
  "GCCompact"
end

#tuning_reportObject



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
55
56
57
58
59
60
61
# File 'lib/autotuner/heuristic/gc_compact.rb', line 28

def tuning_report
  return if @called_gc_compact

  # Don't give suggestion twice
  @called_gc_compact = true

  Report::String.new(<<~MSG)
    The following suggestion runs compaction at boot time, which reduces fragmentation inside of the Ruby heap. This can improve performance and reduce memory usage in forking web servers.

    Before forking your web server, run the following Ruby code:

      3.times { GC.start }
      GC.compact

    For example, with Puma, which runs its before fork hook once on boot (before the initial fork), add the following code into config/puma.rb:

      before_fork do
        3.times { GC.start }
        GC.compact
      end

    With Unicorn, which runs its before fork hook before each fork, add the following code into config/unicorn.rb:

      compacted = false
      before_fork do
        # avoid invalidating heap pages shared with previously forked children
        unless compacted
          3.times { GC.start }
          GC.compact
          compacted = true
        end
      end
  MSG
end