Search code examples
matlabmemory-managementhandle

Matlab handle class handle size


I have a handle class and an array in which I store multiple copies of handles of the class objects I produced (sometimes up to 1000 copies of a single handle). I assumed the handles to the objects themselves would use little memory.

In a post http://es.mathworks.com/matlabcentral/newsreader/view_thread/255117 someone mentioned the handles would be 4 bytes. However if I do a whos on a object handle I get 112 bytes. Which is too much for my current application. Is this normal?

So how do I keep the memory usage down? I can imagine a solution in which I can map the handles (with a container.map) to a uint32 number (4 bytes) and then using that number to represent them in my array of handle copies. It's a bit dirty though because each time I want to get the data from my object I need to go through the mapping.

Any better ideas? Thanks!

As per request an example:

classdef Test < handle
end

>> z = Test();
>> whos z
  Name      Size            Bytes  Class    Attributes
  z         1x1               112  Test  
>> y = [z z z z z z z z z]; %an array of copies of the handle

However this is confusing me:

>> y = [z z z];
>> whos y
  Name      Size            Bytes  Class    Attributes
  y         1x3               128  Test               

>> y = [z z z z];
>> whos y
  Name      Size            Bytes  Class    Attributes
  y         1x4               136  Test  

Solution

  • Whos displays the size of the object including the 4 bytes reference. Your object itself would be 108 bytes and the handle adds another 4 bytes, but the 108 bytes are shared. You can not sum up the memory in whos to get the total allocated memory.

    Take a look at the size of y, it should be numel(y)*4+108