Skip to content

Instantly share code, notes, and snippets.

@wr0ngway
Created March 23, 2012 17:46
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save wr0ngway/2173126 to your computer and use it in GitHub Desktop.
Save wr0ngway/2173126 to your computer and use it in GitHub Desktop.
test to see if GC in ruby 2 is truly copy on write friendly
#!/usr/bin/env ruby
rss = '.+?Rss:\s+(\d+)'
share = '.+?Shared_Clean:\s+(\d+)'
share << '.+?Shared_Dirty:\s+(\d+)'
priv = '.+?Private_Clean:\s+(\d+)'
priv << '.+?Private_Dirty:\s+(\d+)'
MEM_REGEXP = /\[heap\]#{rss}#{share}#{priv}/m
def mem_usage_linux
if File.read("/proc/#{Process.pid}/smaps").match(MEM_REGEXP)
rss = $1.to_i
share = $2.to_i + $3.to_i
priv = $4.to_i + $5.to_i
end
"RSS: #{rss} kB, Shared: #{share} kB, Private: #{priv} kB"
end
def mem_usage_osx
top_output = `top -i1 -l1 -pid #{Process.pid}`
field_names = top_output.lines.to_a[-2].split
raise "Unexpected top output" if field_names.first != 'PID'
field_values = top_output.lines.to_a[-1].split
raise "Unexpected top output" if field_values.first.to_i != Process.pid
fields = Hash[field_names.zip(field_values)]
"RSS: #{fields['RSIZE']}, Shared: #{fields['RSHRD']}, Private: #{fields['RPRVT']}"
end
def mem_usage
case `uname`
when /Darwin/ then mem_usage_osx
when /Linux/ then mem_usage_linux
else raise "Uknown system"
end
end
puts "parent (#{Process.pid}) baseline memory: #{mem_usage}"
alloc=10000 * 2 ** 10
puts "parent allocating #{alloc} objects"
a = []
alloc.times do
a << Object.new
end
puts "parent allocated, mem usage: #{mem_usage}"
GC.start
GC.start
GC.start
puts "parent GC'd, mem usage: #{mem_usage}"
sleep 2
if fork
puts "parent (#{Process.pid}) GC'd and sleeping, mem usage: #{mem_usage}"
sleep 40
else
puts "child (#{Process.pid}) forked, mem usage: #{mem_usage}"
GC.start
GC.start
GC.start
puts "child (#{Process.pid}) GC'd and sleeping, mem usage: #{mem_usage}"
sleep 30
end
$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
$ ./gc-cow.rb
parent (80702) baseline memory: RSS: 4600K+, Shared: 2436K+, Private: 2732K+
parent allocating 10240000 objects
parent allocated, mem usage: RSS: 594M+, Shared: 2436K+, Private: 591M+
parent GC'd, mem usage: RSS: 599M+, Shared: 2436K+, Private: 596M+
child (80710) forked, mem usage: RSS: 1032K+, Shared: 598M+, Private: 192K+
parent (80702) GC'd and sleeping, mem usage: RSS: 599M+, Shared: 598M+, Private: 188K+
child (80710) GC'd and sleeping, mem usage: RSS: 491M+, Shared: 598M+, Private: 6132K+
@lucaspiller
Copy link

Seems to work:

$ ruby -v
ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-darwin12.2.1]
$ ruby gc-cow.rb
parent (25871) baseline memory: RSS: 4292K+, Shared: 1832K+, Private: 2640K+
parent allocating 10240000 objects
parent allocated, mem usage: RSS: 599M+, Shared: 2088K+, Private: 597M+
parent GC'd, mem usage: RSS: 599M+, Shared: 2088K+, Private: 597M+
child (25892) forked, mem usage: RSS: 972K+, Shared: 599M+, Private: 180K+
parent (25871) GC'd and sleeping, mem usage: RSS: 599M+, Shared: 599M+, Private: 180K+
child (25892) GC'd and sleeping, mem usage: RSS: 595M+, Shared: 599M+, Private: 513M+
$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.1]
$ ruby gc-cow.rb
parent (26109) baseline memory: RSS: 5584K+, Shared: 1892K+, Private: 3860K+
parent allocating 10240000 objects
parent allocated, mem usage: RSS: 781M+, Shared: 2240K+, Private: 777M+
parent GC'd, mem usage: RSS: 785M+, Shared: 2240K+, Private: 781M+
child (26130) forked, mem usage: RSS: 1124K+, Shared: 783M+, Private: 296K+
parent (26109) GC'd and sleeping, mem usage: RSS: 785M+, Shared: 783M+, Private: 292K+
child (26130) GC'd and sleeping, mem usage: RSS: 594M+, Shared: 783M+, Private: 88M+

@mark
Copy link

mark commented Oct 18, 2013

Would you mind making this compatible with shard? All you'd need to do is rename the file from gc-cow.shard.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment