How can I tell how much memory a handle object use

2019-04-04 03:34发布

问题:

If I declare 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?

回答1:

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.



回答2:

An easy way I just found is what Dmitry Borovoy suggests here. I wanted to make it available here.

Derive your class from a base class which provides a method, that collects data from all its properties. Code take from given link above.

    function total_mem = get_mem(obj) 
        %// Get all properties
        props = properties(obj); 

        total_mem = 0;
        %// Loop properties
        for ii=1:length(props)
            %// Make shallow copy
            curr_prop = obj.(props{ii});  %#ok<*NASGU>
            %// Get info struct for current property
            s = whos('curr_prop');
            %// Add to total memory consumption
            total_mem = total_mem + s.bytes; 
        end
    end

Sample usage:

>> fprintf('%.1f MB in use.\n',do.sde.get_mem/1024^2)
7413.0 MB in use.