Skip to content

Instantly share code, notes, and snippets.

@paul-r-ml
Created December 18, 2010 23:32
Show Gist options
  • Save paul-r-ml/746969 to your computer and use it in GitHub Desktop.
Save paul-r-ml/746969 to your computer and use it in GitHub Desktop.
REE leaks memory when handling big strings. Run this script with ruby ree and string_size >= 7 to see it.
### usage : ruby(version) mem.rb STRING_SIZE [cow]
### 1 < string_size < 9
### cow to activate copy on write (ree only)
def ram_usage
`pmap #{Process.pid} | tail -1`.split[1][0..-2].to_i
end
def gc_and_log(message, allocate, free)
b = ram_usage
puts format("Running test %s", message)
puts format("Before allocation : %sk", b)
puts "Allocating ..."
allocate.call
puts format("After allocation, before GC : %sk", ram_usage)
free.call
GC.start
a = ram_usage
puts format("After GC : %sk -- %sk leaked", a, a - b)
puts
end
SIZE = ARGV.pop.to_i
if GC.respond_to?(:copy_on_write_friendly=) and ARGV.pop == "cow"
puts "enabling REE copy on write"
GC.copy_on_write_friendly = true
end
t = []
gc_and_log("10**#{SIZE} abc strings",
lambda { 10**SIZE.times {|i| t[i] = "abc"} },
lambda { t = nil })
a = ""
gc_and_log("single 10**#{SIZE} times abc long string",
lambda { a = "abc" * (10**SIZE) },
lambda { a = nil })
puts "press enter to quit"
gets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment