views:

238

answers:

1

If I delcare an object to be a subclass of handle classdef obj < handle my object is now essentially a "pointer" to some memory somewhere. How do I find out how much memory my object is using up?

For example, say I have a class foo with a field bar

classdef foo < handle 
properties
    bar = randn(1000);
end

bar takes up 8 megabytes (8 bytes * 1 million numbers)

but if I type

obj = foo();
whos('obj');

I get

Name      Size            Bytes  Class    Attributes

  obj      1x1                60  foo                

How do I find out how much total memory obj points to?

+3  A: 

As a hack, convert it to a struct and see how much space that takes up. I think that will expose all the data in "regular" object fields.

f = foo();
origWarn = warning();
warning off 'MATLAB:structOnObject'
s = builtin('struct', f); % use 'builtin' in case @foo overrides struct()
warning(origWarn);

Then you can see it in whos.

>> whos
  Name      Size              Bytes  Class     Attributes

  f         1x1                  60  foo                 
  s         1x1             8000124  struct       

This is just a first order approximation. It will tell you how much memory its fields are using. If any of them contain handle objects, you need to recurse down the fields of that struct and convert any other handle objects to struct to count their fields. (If you want to include Java objects' memory, you'll also need a separate function to estimate their storage size. Probably not worth the bother.) Now that Matlab has closures, function handles may also contain data; you'll need to punch in to those using functions() if you want to count closed-over data.

If you're working with handle objects, you may have aliasing and even circular references at the M-code level, so you'd need to watch out for that when recursing. (Sorry, I don't know how to handle that in the new OO system.)

The memory display in whos will also double-count arrays that are sharing memory via Matlab's copy-on-write optimization. Here's a concrete example.

x = NaN(1,10000);
s.x = x;
s.y = x;
s.z = x;


>> whos
  Name      Size                Bytes  Class     Attributes

  s         1x1                240372  struct              
  x         1x10000             80000  double              

In reality, s is only consuming about 80K; it just contains three pointers to x. And that 80K is the same 80K that x itself is consuming. Unless you modify any of them; then a new array is allocated. Whos() won't let you differentiate those cases. Handling this is hard; AFAIK the only way to do this is to use a MEX file to get the mxarray's data pointer(s) and walk the object tree yourself, detecting aliased pointers and counting the aliased bytes.

This is a general problem with measuring the size of objects in memory when their components could be shared. They're not discrete physical objects. At least you're not in C, working with pointers to arbitrary memory blocks.

Andrew Janke
Thanks! That's a really good suggestion. One question, because I don't understand how matlab decides to allocate new memory and copy: How much memory will converting foo to a struct take up? In other words, if foo has a 1 GB field, will converting it to a struct cause MATLAB to consume an extra GB of memory?
Marc
How would you estimate the size of Java objects? - these are reported in Matlab as having 0 bytes...
Yair Altman
@Marc: Converting foo to a struct will not use much memory. Just the struct itself is converted. The contents of the struct's fields, including that hypothetical 1 GB field, will each be shared using matlab's copy-on-write optimization.
Andrew Janke
@Yair Altman: You need to call Java code to estimate the size of Java objects, invoking it from the Matlab code when isjava() says you're looking at a Java object. No standard Java SDK calls do this AFAIK. I've used JavaWorld's "sizeof for Java" library (http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html?page=5) with a little M-code wrapper on top of it. New 1.6 JVMs might have better instrumentation you could use, maybe by calling profiling hooks from within the JVM.
Andrew Janke

related questions