Re: Object's allocated memory



crb002@xxxxxxxxx wrote:
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.

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]

.



Relevant Pages

  • Re: Lot of classes
    ... >subclass for every Utility that I have, because every class has a big ... usage data that indicates it's a problem. ... Now, if you want to reduce the memory footprint of your app, perhaps ...
    (microsoft.public.dotnet.framework)
  • Re: OT: Who will switch to (or at least try out) Opera now that they have free registrations?
    ... memory footprint it doesn't make it quick. ... I do concede that small footprint probably means faster start up time tho:) ... > Mozilla suite, - browser, mail, calender, gives me all I need for a ... > small business setting. ...
    (Debian-User)