Skip to content

Instantly share code, notes, and snippets.

@floere
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floere/8843872 to your computer and use it in GitHub Desktop.
Save floere/8843872 to your computer and use it in GitHub Desktop.
Quick hash memory test to understand high memory usage when using hashes in Ruby 1.9.3 and 2.1.0 (regarding newly created strings and old hashes).
# We repeatedly fill a hash.
# We use the same keyspace each time after the first time.
#
# My expectation was that the total memory used would not increase significantly after the first iteration.
#
h = {}
10.times do |i|
100000.times do |i|
h[i.to_s] = [1,2,3]
end
puts "#{"%5i" % i} #{`ps aux | grep -e 'ruby' | grep -v grep`}"
end
# We repeatedly fill a hash.
# We use the same keyspace each time after the first time.
#
# My expectation was that the total memory used would not increase significantly after the first iteration.
#
# Note: In this one, we also clear the hash before filling it.
#
h = {}
10.times do |i|
h.clear
100000.times do |i|
h[i.to_s] = nil
end
puts "#{"%5i" % i} #{`ps aux | grep -e 'ruby' | grep -v grep`}"
end
# This time we also freeze the string key.
#
h = {}
10.times do |i|
100000.times do |i|
h[%q{i.to_s}.freeze] = [1,2,3]
end
puts "#{"%5i" % i} #{`ps aux | grep -e 'ruby' | grep -v grep`}"
end
hanke@soyuz:hashmemtest: chruby 1.9.3
hanke@soyuz:hashmemtest: ruby hashmemtest1.rb
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
0 hanke 11035 5.1 0.1 2469184 19280 s006 S+ 2:26PM 0:00.10 ruby hashmemtest1.rb
1 hanke 11035 32.4 0.1 2469184 22572 s006 S+ 2:26PM 0:00.17 ruby hashmemtest1.rb
2 hanke 11035 47.7 0.1 2478400 22592 s006 S+ 2:26PM 0:00.24 ruby hashmemtest1.rb
3 hanke 11035 56.4 0.1 2478400 22592 s006 S+ 2:26PM 0:00.31 ruby hashmemtest1.rb
4 hanke 11035 60.5 0.1 2478400 22592 s006 S+ 2:26PM 0:00.38 ruby hashmemtest1.rb
5 hanke 11035 60.5 0.1 2478400 22592 s006 S+ 2:26PM 0:00.45 ruby hashmemtest1.rb
6 hanke 11035 59.5 0.1 2478400 22592 s006 S+ 2:26PM 0:00.52 ruby hashmemtest1.rb
7 hanke 11035 63.7 0.1 2478400 22592 s006 S+ 2:26PM 0:00.58 ruby hashmemtest1.rb
8 hanke 11035 67.9 0.1 2478400 22592 s006 S+ 2:26PM 0:00.65 ruby hashmemtest1.rb
9 hanke 11035 71.5 0.1 2478400 22592 s006 S+ 2:26PM 0:00.73 ruby hashmemtest1.rb
hanke@soyuz:hashmemtest: ruby hashmemtest2.rb
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
0 hanke 11084 5.6 0.1 2460992 14860 s006 S+ 2:26PM 0:00.10 ruby hashmemtest2.rb
1 hanke 11084 32.5 0.1 2470208 17804 s006 S+ 2:26PM 0:00.17 ruby hashmemtest2.rb
2 hanke 11084 48.4 0.1 2470208 18232 s006 S+ 2:26PM 0:00.25 ruby hashmemtest2.rb
3 hanke 11084 59.1 0.1 2470208 18496 s006 S+ 2:26PM 0:00.33 ruby hashmemtest2.rb
4 hanke 11084 64.0 0.1 2471232 21528 s006 S+ 2:26PM 0:00.40 ruby hashmemtest2.rb
5 hanke 11084 64.0 0.1 2471232 22444 s006 S+ 2:26PM 0:00.47 ruby hashmemtest2.rb
6 hanke 11084 62.1 0.1 2472256 22456 s006 S+ 2:26PM 0:00.55 ruby hashmemtest2.rb
7 hanke 11084 61.9 0.1 2472256 22460 s006 S+ 2:26PM 0:00.62 ruby hashmemtest2.rb
8 hanke 11084 69.9 0.1 2472256 22464 s006 S+ 2:26PM 0:00.70 ruby hashmemtest2.rb
9 hanke 11084 69.7 0.1 2472256 22464 s006 S+ 2:26PM 0:00.77 ruby hashmemtest2.rb
hanke@soyuz:hashmemtest: ruby hashmemtest3.rb
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
0 hanke 11133 3.1 0.0 2465852 5828 s006 S+ 2:26PM 0:00.06 ruby hashmemtest3.rb
1 hanke 11133 26.1 0.0 2475068 5852 s006 S+ 2:26PM 0:00.09 ruby hashmemtest3.rb
2 hanke 11133 26.1 0.0 2475068 5852 s006 S+ 2:26PM 0:00.12 ruby hashmemtest3.rb
3 hanke 11133 36.4 0.0 2475068 5852 s006 S+ 2:26PM 0:00.16 ruby hashmemtest3.rb
4 hanke 11133 36.4 0.0 2475068 5856 s006 S+ 2:26PM 0:00.19 ruby hashmemtest3.rb
5 hanke 11133 39.1 0.0 2475068 5856 s006 S+ 2:26PM 0:00.23 ruby hashmemtest3.rb
6 hanke 11133 39.1 0.0 2475068 5856 s006 S+ 2:26PM 0:00.27 ruby hashmemtest3.rb
7 hanke 11133 43.2 0.0 2475068 5856 s006 S+ 2:26PM 0:00.30 ruby hashmemtest3.rb
8 hanke 11133 43.2 0.0 2475068 5856 s006 S+ 2:26PM 0:00.33 ruby hashmemtest3.rb
9 hanke 11133 44.4 0.0 2475068 5856 s006 S+ 2:26PM 0:00.37 ruby hashmemtest3.rb
hanke@soyuz:hashmemtest: chruby 2.1.0
hanke@soyuz:hashmemtest: ruby hashmemtest1.rb
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
0 hanke 11192 26.2 0.2 2469972 25800 s006 S+ 2:26PM 0:00.12 ruby hashmemtest1.rb
1 hanke 11192 45.5 0.2 2479188 34112 s006 S+ 2:26PM 0:00.19 ruby hashmemtest1.rb
2 hanke 11192 45.5 0.3 2495572 43824 s006 S+ 2:26PM 0:00.26 ruby hashmemtest1.rb
3 hanke 11192 49.5 0.3 2495572 43824 s006 S+ 2:26PM 0:00.32 ruby hashmemtest1.rb
4 hanke 11192 54.9 0.3 2495572 43832 s006 S+ 2:26PM 0:00.39 ruby hashmemtest1.rb
5 hanke 11192 64.6 0.3 2495572 43832 s006 S+ 2:26PM 0:00.46 ruby hashmemtest1.rb
6 hanke 11192 67.2 0.3 2511956 53348 s006 S+ 2:26PM 0:00.54 ruby hashmemtest1.rb
7 hanke 11192 70.9 0.4 2528340 65316 s006 S+ 2:26PM 0:00.64 ruby hashmemtest1.rb
8 hanke 11192 71.3 0.4 2528340 69452 s006 S+ 2:26PM 0:00.71 ruby hashmemtest1.rb
9 hanke 11192 71.3 0.4 2528340 69452 s006 S+ 2:26PM 0:00.77 ruby hashmemtest1.rb
hanke@soyuz:hashmemtest: ruby hashmemtest2.rb
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
0 hanke 11241 23.1 0.1 2460756 19944 s006 S+ 2:26PM 0:00.12 ruby hashmemtest2.rb
1 hanke 11241 44.2 0.2 2460756 25648 s006 S+ 2:26PM 0:00.20 ruby hashmemtest2.rb
2 hanke 11241 53.2 0.2 2469972 30812 s006 S+ 2:26PM 0:00.29 ruby hashmemtest2.rb
3 hanke 11241 62.5 0.2 2469972 30844 s006 S+ 2:26PM 0:00.38 ruby hashmemtest2.rb
4 hanke 11241 65.2 0.2 2469972 30852 s006 S+ 2:26PM 0:00.47 ruby hashmemtest2.rb
5 hanke 11241 67.4 0.2 2469972 30868 s006 S+ 2:26PM 0:00.55 ruby hashmemtest2.rb
6 hanke 11241 70.0 0.2 2469972 30868 s006 S+ 2:26PM 0:00.64 ruby hashmemtest2.rb
7 hanke 11241 71.1 0.2 2469972 31880 s006 S+ 2:26PM 0:00.72 ruby hashmemtest2.rb
8 hanke 11241 71.0 0.2 2469972 31888 s006 S+ 2:26PM 0:00.81 ruby hashmemtest2.rb
9 hanke 11241 71.0 0.2 2469972 31888 s006 S+ 2:26PM 0:00.89 ruby hashmemtest2.rb
hanke@soyuz:hashmemtest: ruby hashmemtest3.rb
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
0 hanke 11290 0.0 0.0 2448208 7344 s006 S+ 2:26PM 0:00.05 ruby hashmemtest3.rb
1 hanke 11290 15.9 0.0 2448208 7344 s006 S+ 2:26PM 0:00.07 ruby hashmemtest3.rb
2 hanke 11290 15.9 0.0 2457424 7364 s006 S+ 2:26PM 0:00.09 ruby hashmemtest3.rb
3 hanke 11290 19.8 0.0 2457424 7364 s006 S+ 2:26PM 0:00.10 ruby hashmemtest3.rb
4 hanke 11290 19.8 0.0 2457424 7364 s006 S+ 2:26PM 0:00.12 ruby hashmemtest3.rb
5 hanke 11290 27.0 0.0 2457424 7364 s006 S+ 2:26PM 0:00.14 ruby hashmemtest3.rb
6 hanke 11290 27.0 0.0 2466640 7384 s006 S+ 2:26PM 0:00.15 ruby hashmemtest3.rb
7 hanke 11290 27.0 0.0 2466640 7384 s006 S+ 2:26PM 0:00.17 ruby hashmemtest3.rb
8 hanke 11290 26.7 0.0 2466640 7384 s006 S+ 2:26PM 0:00.18 ruby hashmemtest3.rb
9 hanke 11290 26.7 0.0 2466640 7384 s006 S+ 2:26PM 0:00.20 ruby hashmemtest3.rb
# E.g. run using chruby
#
chruby 1.9.3
ruby hashmemtest1.rb
ruby hashmemtest2.rb
ruby hashmemtest3.rb
chruby 2.1.0
ruby hashmemtest1.rb
ruby hashmemtest2.rb
ruby hashmemtest3.rb
# Compare the memory usage and how it increases.
# Is it as you expected?
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment