How to find out array size of an array?
I don’t need the length but want to know how much a array takes up in
memory.
Thanks
Tridib
How to find out array size of an array?
I don’t need the length but want to know how much a array takes up in
memory.
Thanks
Tridib
On 08/30/2012 07:10 PM, Tridib B. wrote:
How to find out array size of an array?
I don’t need the length but want to know how much a array takes up in
memory.
Including or excluding the memory footprint of the objects in the array?
What about objects references by objects in the array, the objects
referenced by those objects, and so on?
Also, what Ruby implementation and version are we talking about, and on
what platform?
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 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.
Array less than 4 elements is embedded into RObject and takes no
additional size to store(that’s not related to array values per se,
since they have own RObject instances)
So for instance, you have array a = [1,2,3,4]
The raw store space is 32 bytes(array > 3 overhead) + array RObject
size, + RObject size * 4 (number of int elements)
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs