Reliably determine RAM consumption of MATLAB variables

48 views (last 30 days)
I am looking for a reliable way to determine how much RAM is devoted to Matlab variable data at a given moment. The whos() command does not work, because it doesn't account for copy-on-write sharing. For example, this code results in 2MB of Matlab data, but whos() counts it as 4MB,
whos A B
Name Size Bytes Class Attributes A 512x512 2097152 double B 512x512 2097152 double
Likewise the memory() command is not reliable, because it requests a memory tally from the operating system, whose estimate can fluctuate from moment to moment depending on what the operating system is doing in the background.
There must be a way to do this, right?
Matt J
Matt J on 29 Feb 2024
Edited: Matt J on 1 Mar 2024
@Walter Roberson I don't know if you're referring to a doc, but it still doesn't resolve the contradiction I presented to you in my previous post. According to your formula, m3 is correct, but that means memory() gave unreliable results. Which do we trust, and why?
Matt J
Matt J on 1 Mar 2024
Edited: Matt J on 1 Mar 2024
@Walter Roberson the fact that ValuesForCopy exists doesn't prove that griddedInterpolant legitimately takes up twice as much memory. They could be shallow copies of one another. In fact, it seems the likely explanation for why memory() is a factor of 2 in disagreement with whos().

Sign in to comment.

Answers (0)


Find more on Numeric Types in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!