Object's allocated memory

C

crb002

I have an application where I need to sort objects based on their
memory footprint. What is the easiest way to get this information
from Ruby1.6 ? I have no problem with having to call some C functions
to get this info.

The hack Marshal.dump(foo).size() would work, but since this might be
called often I need an elegant solution.
 
T

Tim Hunter

I have an application where I need to sort objects based on their
memory footprint. What is the easiest way to get this information
from Ruby1.6 ? I have no problem with having to call some C functions
to get this info.

The hack Marshal.dump(foo).size() would work, but since this might be
called often I need an elegant solution.

Hmmm...tricky. Seems like you'd want a #footprint method in every class
that accumlated the memory footprint of the object. That method would be
written in C so it could account for the sizes of the C structure
(RString, RArray, etc.) that represents the object. You'll have to make
some rules about collections. Does the footprint of an array include the
footprints of the object in the array, or just the container? Same for
Hash, etc. Then there's going to be some objects for which you just
can't get an accurate count. I suspect it would be hard to find out how
much memory is allocated to buffers for an open file, for example (if
that matters). And I know for a fact that you can't find out how much
memory is allocated to one of RMagick's Magick::Image objects. Even the
Marshal hack wouldn't work there since the on-disk size doesn't have to
match the in-memory size. Of course your application may not use such
objects, but they exist.
 
K

Konrad Meyer

--nextPart3904477.pDEVkUUck2
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth (e-mail address removed):
I have an application where I need to sort objects based on their
memory footprint. What is the easiest way to get this information
from Ruby1.6 ? I have no problem with having to call some C functions
to get this info.
=20
The hack Marshal.dump(foo).size() would work, but since this might be
called often I need an elegant solution.

You also might want to fix your Ruby1.6 problem. 1.8.6 has been out for a
while.

=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart3904477.pDEVkUUck2
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBG9sBVCHB0oCiR2cwRAnLZAJ4pxFjyUGtxkJng7i7F2zQwzWCT5wCgtdzr
j0qacXwNYl/AGQ9FM6mhgvc=
=AZqN
-----END PGP SIGNATURE-----

--nextPart3904477.pDEVkUUck2--
 
E

Eric Hodel

I have an application where I need to sort objects based on their
memory footprint. What is the easiest way to get this information
from Ruby1.6 ? I have no problem with having to call some C functions
to get this info.

The hack Marshal.dump(foo).size() would work, but since this might be
called often I need an elegant solution.

Look at the mem_inspect gem. It has hooks into the GC for Ruby 1.8.

Marshal.dump(foo).size will include all the memory an object
references, including the object's instance variables & etc. The
Array ['x'*1024] takes up only a few bytes of memory, but
Marshal.dump.size says 1032 bytes.
 
R

Robert Klemme

I have an application where I need to sort objects based on their
memory footprint. What is the easiest way to get this information
from Ruby1.6 ? I have no problem with having to call some C functions
to get this info.

The hack Marshal.dump(foo).size() would work, but since this might be
called often I need an elegant solution.

Look at the mem_inspect gem. It has hooks into the GC for Ruby 1.8.

Marshal.dump(foo).size will include all the memory an object references,
including the object's instance variables & etc. The Array ['x'*1024]
takes up only a few bytes of memory, but Marshal.dump.size says 1032 bytes.

And that's the problem with all those memory inspection issues:
virtually no automated algorithm (that I know of) can automatically
decide which part of the object graph is supposed to be counted as
belonging to another object. And then there's still aliasing effects,
i.e. objects can be referenced by any number of other objects.

OP, what problem are you trying to solve?

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top