Home > manopt > tools > getsize.m

getsize

PURPOSE ^

Estimates the amount of memory a given variable occupies, in bytes.

SYNOPSIS ^

function bytes = getsize(variable)

DESCRIPTION ^

 Estimates the amount of memory a given variable occupies, in bytes.

 Source:
 https://ch.mathworks.com/matlabcentral/answers/14837-how-to-get-size-of-an-object
 Posted by Dmitry Borovoy on Matlab Answers on Aug. 31, 2011.
 Extended to structs by Mario Reutter on Matlab Answers on Dec. 13, 2017.
 Slightly adapted by Victor Liao, Aug. 2022.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function bytes = getsize(variable)
0002 % Estimates the amount of memory a given variable occupies, in bytes.
0003 %
0004 % Source:
0005 % https://ch.mathworks.com/matlabcentral/answers/14837-how-to-get-size-of-an-object
0006 % Posted by Dmitry Borovoy on Matlab Answers on Aug. 31, 2011.
0007 % Extended to structs by Mario Reutter on Matlab Answers on Dec. 13, 2017.
0008 % Slightly adapted by Victor Liao, Aug. 2022.
0009 
0010 % This file is part of Manopt: www.manopt.org.
0011 % Original authors: Dmitry Borovoy and Mario Reutter
0012 % Contributors: Victor Liao
0013 % Change log:
0014 
0015     props = properties(variable); 
0016     if size(props, 1) < 1
0017         bytes = whos(varname(variable));
0018         bytes = bytes.bytes;
0019     else % code of Dmitry
0020       bytes = 0;
0021       for ii = 1:length(props)
0022           currentProperty = getfield(variable, char(props(ii)));
0023           s = whos(varname(currentProperty));
0024           bytes = bytes + s.bytes;
0025       end
0026     end
0027 end
0028 
0029 function name = varname(~)
0030     name = inputname(1);
0031 end

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005