The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
The dangers of #undef_method, #instance_exec recalled for memleaking!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
The dangers of #undef_method, #instance_exec recalled for memleaking! Posted: Jul 10, 2006 3:34 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: The dangers of #undef_method, #instance_exec recalled for memleaking!
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

After reading this thread on ruby-talk, I reviewed my instance_exec implementation and found it totally unacceptable. I used #undef_method instead of #remove_method, what a fool I was! I'll first show why it can be a problem and then explain the causes; the problem is quite general, affecting a number of meta-hacks.

Take this script that just defines lots of methods to undefine them right away, measuring the VmSize as it goes*1

ITER = 10

def vm_size
  File.read("/proc/#{Process.pid}/status")[/^VmSize:\s+(\d+) kB/, 1].to_i
end

vm1 = vm_size
puts "I'm #{Process.pid}, using #{vm1} kB."
b = lambda{}
ITER.times do |i|
  (i*10000...(i+1)*10000).each do |i|
    name = "foo%6d" % i
    Object.module_eval{ define_method(name, &b); undef_method name }
  end
  GC.start
end
inc = vm_size - vm1
puts "undef_method"
puts "Increment: #{inc}, #{inc * 1024 / (ITER * 10000)} bytes per method."

Here's the output:

$ ruby undef_method.rb 
I'm 1343, using 3020 kB.
undef_method
Increment: 23092, 118 bytes per method.

The script is saying that each method definition is taking around 120 bytes, even when the method is removed right away.*2

This is not surprising: the memleak is caused by the symbols. But that's not all. I claimed that #undef_method was worse than #remove_method, and here's the proof: running the above script after substituting undef_method with remove_method yields this:

$ ruby remove_method.rb 
I'm 3170, using 3020 kB.
remove_method
Increment: 6932, 70 bytes per method.

The increment per method remains consistently at least 40+ bytes smaller than for #undef_method, so there's something besides symbol leaking.

How undef_method works

The difference lies in how remove_method and undef_method works.


Read more...

Read: The dangers of #undef_method, #instance_exec recalled for memleaking!

Topic: Best Remix EVAR! Previous Topic   Next Topic Topic: JRuby 0.9.0: Ruby on Rails on Java - Sweet!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use