Skip to content

Instantly share code, notes, and snippets.

@wr0ngway
Created March 23, 2012 17:46
Show Gist options
  • 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+
@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