From: Robert Klemme Date: 2012-08-31T22:56:17+09:00 Subject: Re: Array Size in Ruby On Fri, Aug 31, 2012 at 2:57 PM, Sigurd 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/