module TryCatch::Memory
Public Class Methods
clean_memory_by_object_id(object_id, thread= Thread.current)
click to toggle source
# File lib/try-catch/memory.rb, line 55 def clean_memory_by_object_id object_id, thread= Thread.current init_memory_allocation t: thread, o: object_id if @@memoryspace[thread][object_id].count >= 20 ( @@memoryspace[thread][object_id].count - 10 ).times{ @@memoryspace[thread][object_id].shift } end end
clean_thread_memory(thread= Thread.current)
click to toggle source
# File lib/try-catch/memory.rb, line 64 def clean_thread_memory thread= Thread.current init_memory_allocation t: thread @@memoryspace[thread].keys.each{ |object_id| clean_memory_by_object_id(object_id) } end
init_memory_allocation(opts={})
click to toggle source
# File lib/try-catch/memory.rb, line 7 def init_memory_allocation opts={} raise(ArgumentError) unless opts.class <= Hash opts[:thread] ||= opts.delete(:t) || Thread.current opts[:object_id] ||= opts.delete(:o) || opts.delete(:object) @@memoryspace[opts[:thread]] ||= {} unless opts[:object_id].nil? @@memoryspace[opts[:thread]][opts[:object_id]] ||= [] end return nil end
load(object_id)
click to toggle source
loaded_exs= @@memoryspace return nil if loaded_exs.nil? return loaded_exs.pop unless loaded_exs.class != Array
# File lib/try-catch/memory.rb, line 38 def load object_id return nil if @@memoryspace[Thread.current].nil? return @@memoryspace[Thread.current][object_id].last end
memory_agent()
click to toggle source
# File lib/try-catch/memory.rb, line 75 def memory_agent @@memory_agent ||= nil if @@memory_agent.nil? @@memory_agent= ::Thread.new{ loop{ sleep(60) Memory.memory_clean } } end return @@memory_agent end
memory_clean()
click to toggle source
# File lib/try-catch/memory.rb, line 71 def memory_clean Thread.list.each{ |thr| clean_thread_memory(thr) } end
pop(object_id)
click to toggle source
# File lib/try-catch/memory.rb, line 45 def pop object_id return nil if @@memoryspace[Thread.current].nil? unless @@memoryspace[Thread.current][object_id].nil? @@memoryspace[Thread.current][object_id].pop end end
save(object_id, exception)
click to toggle source
# File lib/try-catch/memory.rb, line 21 def save object_id, exception begin init_memory_allocation o: object_id @@memoryspace[Thread.current][object_id].push exception return true rescue ArgumentError return false end end