Merge branch 'Raven24-memory-control'

This commit is contained in:
Maxwell Salzberg 2012-04-05 14:29:52 -07:00
commit 8de9ce9963
3 changed files with 25 additions and 5 deletions

View file

@ -104,8 +104,8 @@ end
# https://makandracards.com/makandra/950-speed-up-rspec-by-deferring-garbage-collection
require File.join(File.dirname(__FILE__), "..", "..", "spec", "support", "deferred_garbage_collection")
Before do
DeferredGarbageCollection.start unless ENV['TRAVIS']
DeferredGarbageCollection.start
end
After do
DeferredGarbageCollection.reconsider unless ENV['TRAVIS']
DeferredGarbageCollection.reconsider
end

View file

@ -105,9 +105,9 @@ end
# https://makandracards.com/makandra/950-speed-up-rspec-by-deferring-garbage-collection
RSpec.configure do |config|
config.before(:all) do
DeferredGarbageCollection.start unless ENV['TRAVIS']
DeferredGarbageCollection.start
end
config.after(:all) do
DeferredGarbageCollection.reconsider unless ENV['TRAVIS']
DeferredGarbageCollection.reconsider
end
end

View file

@ -7,11 +7,27 @@ class DeferredGarbageCollection
@@last_gc_run = Time.now
def self.start
return if unsupported_enviroment
GC.disable if DEFERRED_GC_THRESHOLD > 0
end
def self.memory_threshold
mem = %x(free 2>/dev/null).to_s.split(" ")
return nil if mem.empty?
mem[8].to_i / (mem[7].to_i/100)
end
def self.reconsider
if DEFERRED_GC_THRESHOLD > 0 && Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD
return if unsupported_enviroment
if (percent_used = self.memory_threshold)
running_out_of_memory = percent_used > 90
puts "percent memory used #{percent_used}" # just for info, as soon as we got some numbers remove it
else
running_out_of_memory = false
end
if( (DEFERRED_GC_THRESHOLD > 0 && Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD) || running_out_of_memory )
GC.enable
GC.start
GC.disable
@ -19,4 +35,8 @@ class DeferredGarbageCollection
end
end
def self.unsupported_enviroment
# ENV['TRAVIS']
end
end