On Fri, Aug 31, 2012 at 2:57 PM, Sigurd <[email protected]> wrote:
> If you use ruby 1.9.2 or later you can try following approach:
> require 'objspace'
>
> ObjectSpace.memsize_of(array).tap do |size|
>         array.each do |el|
>                 size += ObjectSpace.memsize_of(el)
>         end
> end

That does not work since tap will return the result of memsize_of(array) only.

irb(main):003:0> 123.tap {|s| s + 999}
=> 123
irb(main):004:0> 123.tap {|s| s += 999}
=> 123

> That you'll give you size of memory taken. But that does not take into 
> account the size of ruby RObject internal struct that is referencing every 
> single ruby object.  It's size depends on 32/64 bit platform and packing 
> options. My guess that it's around 128 bytes.

It also does not take into account memory taken by all referenced
objects since you descend one level only.  You would need a DFS of BFS
to properly account for all levels.

Kind regards

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