Ruby 2.1.7 vs 2.2.3垃圾收集
我看到一些奇怪的潜在ruby 2.2.3 gc相关问题,从单词列表中生成前缀。可疑代码如下:Ruby 2.1.7 vs 2.2.3垃圾收集,ruby,memory-leaks,garbage-collection,Ruby,Memory Leaks,Garbage Collection,我看到一些奇怪的潜在ruby 2.2.3 gc相关问题,从单词列表中生成前缀。可疑代码如下: def insert(word) return if word.nil? || word.empty? node = self counter = 0 l = word.length #TODO _ why is this not being garbage collected? word.bytes.each do |i|
def insert(word)
return if word.nil? || word.empty?
node = self
counter = 0
l = word.length
#TODO _ why is this not being garbage collected?
word.bytes.each do |i|
counter += 1
node.children[i] ||= Node.new(i.chr, node, counter == l)
node = node.children[i]
node.terminal = true if counter == l
end
end
它是从字符串列表中调用的,因此
words.each_with_index do |word, i|
@root.insert(word)
当我对此进行一些内存分析时,我看到ruby 2.2.3-railsexpress中的总内存使用量为~68MB:
Measure Mode: memory
Thread ID: 70102347546980
Fiber ID: 70102348363420
Total: 68688.375000
Sort by: self_time
%self total self wait child calls name
20.03 13755.000 13755.000 0.000 0.000 293398 FastAutocomplete::Node#initialize
9.05 6218.875 6218.875 0.000 0.000 86036 String#bytes
6.26 66665.562 4298.438 0.000 62367.125 86040 *Array#each
1.50 1028.969 1028.969 0.000 0.000 230540 Hash#keys
0.98 677.219 676.438 0.000 0.781 1 Array#map
Measure Mode: memory
Thread ID: 70216772377340
Fiber ID: 70216782765400
Total: 22009.125000
Sort by: self_time
%self total self wait child calls name
62.49 13754.391 13754.391 0.000 0.000 293398 FastAutocomplete::Node#initialize
28.25 6218.641 6218.641 0.000 0.000 86036 String#bytes
4.67 1028.078 1028.078 0.000 0.000 230540 Hash#keys
3.07 676.797 676.203 0.000 0.594 1 Array#map
1.27 280.203 280.203 0.000 0.000 116093 Thread::Queue#enq
0.07 19.297 15.516 0.000 3.781 9 JSON::Ext::Generator::GeneratorMethods::Hash#to_json
当我使用2.1.7时,我只看到大约22MB的使用量:
Measure Mode: memory
Thread ID: 70102347546980
Fiber ID: 70102348363420
Total: 68688.375000
Sort by: self_time
%self total self wait child calls name
20.03 13755.000 13755.000 0.000 0.000 293398 FastAutocomplete::Node#initialize
9.05 6218.875 6218.875 0.000 0.000 86036 String#bytes
6.26 66665.562 4298.438 0.000 62367.125 86040 *Array#each
1.50 1028.969 1028.969 0.000 0.000 230540 Hash#keys
0.98 677.219 676.438 0.000 0.781 1 Array#map
Measure Mode: memory
Thread ID: 70216772377340
Fiber ID: 70216782765400
Total: 22009.125000
Sort by: self_time
%self total self wait child calls name
62.49 13754.391 13754.391 0.000 0.000 293398 FastAutocomplete::Node#initialize
28.25 6218.641 6218.641 0.000 0.000 86036 String#bytes
4.67 1028.078 1028.078 0.000 0.000 230540 Hash#keys
3.07 676.797 676.203 0.000 0.594 1 Array#map
1.27 280.203 280.203 0.000 0.000 116093 Thread::Queue#enq
0.07 19.297 15.516 0.000 3.781 9 JSON::Ext::Generator::GeneratorMethods::Hash#to_json
在整个创建过程中,我尝试了多种方法,比如跟踪GC.stats,但没有发现任何特别有用的方法。我还尝试使用string.each_char或string.split(“”).each(即不同的迭代器)
关于2.2.3使用3倍内存的原因有什么想法吗?这导致我们的一些服务器爆炸