Class: Autogc

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

Overview

This class disables garbage collection

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Autogc

Disables the normal GC and enables a timeout to automatically GC.



4
5
6
7
8
9
10
11
# File 'lib/autogc.rb', line 4

def initialize(args = {})
  @args = args
  
  puts "Starting Autogc." if @args[:debug]
  @args[:time] = 1
  @thread = Thread.new(&self.method(:gc_loop))
  GC.disable
end

Class Method Details

.enable_for_known_buggy_env(args = {}) ⇒ Object

Starts autogc if it is running on a known buggy environment.



14
15
16
17
18
19
# File 'lib/autogc.rb', line 14

def self.enable_for_known_buggy_env(args = {})
  found = false
  found = true if RUBY_VERSION == "1.9.3"
  Autogc.new(args) if found
  return found
end

Instance Method Details

#gcObject

Execute garbage-collection exclusive.



40
41
42
43
44
45
46
47
# File 'lib/autogc.rb', line 40

def gc
  Thread.exclusive do
    puts "Autogc: Doing garbage collection." if @args[:debug]
    GC.enable
    GC.start
    GC.disable
  end
end

#gc_loopObject

Starts the loop that executes the garbage-collection.



28
29
30
31
32
33
34
35
36
37
# File 'lib/autogc.rb', line 28

def gc_loop
  loop do
    begin
      sleep @args[:time]
      self.gc
    rescue => e
      puts "Autogc: An error occurred while triggering the garbage-collection."
    end
  end
end

#stopObject

Stops the garbage-collection on time and enables the normal GC.



22
23
24
25
# File 'lib/autogc.rb', line 22

def stop
  GC.enable
  @thread.kill
end