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 `<main>'

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

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to