On Mon, Dec 31, 2012 at 3:43 AM, Edward QU [email protected] wrote:
I got a error message while I was running a multi-threads program.
the number of line is less than 500.
I can make a program crash for memory reasons in far less lines.
$ time ruby -e ‘a=[“"];loop {a << (a.last + "”)}’
-e:1:in block in <main>': failed to allocate memory (NoMemoryError) from -e:1:in
loop’
from -e:1:in `’
real 0m50.809s
user 0m3.041s
sys 0m4.695s
any help would be great appreciated!
Difficult without seeing the whole program and knowing all the
libraries you use. You could insert regular dumps of class statistics
via ObjectSpace in order to get an idea of most allocated objects,
e.g.
stat=Hash.new 0
ObjectSpace.each_object {|o| stat[o.class]+=1}
A bit more involved:
LIB -------------
require ‘monitor’
$stat = Hash.new(0).extend MonitorMixin
def $stat.count(o)
synchronize do
self[caller(4)] += 1
end
o
end
def $stat.dump(n = 10)
synchronize do
sort_by {|s,c| c}.last(n).each do |s, c|
printf “%10d %s\n”, c, s[0…5].join(‘|’)
end
end
end
def replace_new(cl)
cl.class_eval do
alias _new new
def new(*a,&b)
$stat.count(_new(*a,&b))
end
end
cl
end
replace_new Class
class <<Struct
alias _new new
def new(*a, &b)
_new(*a, &b).tap do |cl|
replace_new(cl.singleton_class)
end
end
end
TEST -------------
S = Struct.new :x
class Y
def initialize(x) @x=x end
end
5.times {|i| S.new i
Y.new i+1 }
$stat.dump
Cheers
robert