Module: TryCatch::Memory

Defined in:
lib/try-catch/memory.rb

Constant Summary collapse

@@memoryspace =
{}

Class Method Summary collapse

Class Method Details

.clean_memory_by_object_id(object_id, thread = Thread.current) ⇒ Object



55
56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
68
69
# 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 = {}) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object

loaded_exs= @@memoryspace return nil if loaded_exs.nil? return loaded_exs.pop unless loaded_exs.class != Array



38
39
40
41
42
43
# 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_agentObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_cleanObject



71
72
73
# File 'lib/try-catch/memory.rb', line 71

def memory_clean
  Thread.list.each{ |thr| clean_thread_memory(thr) }
end

.pop(object_id) ⇒ Object



45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# 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