File Exchange Pick of the Week

Our best user submissions

Memory Info 4

Posted by Sean de Wolski,

Sean‘s pick this week is Memory Info by Sviatoslav Danylenko.

Have you ever wanted to get the current memory used by MATLAB? I do this occassionally at the command line and have done this in code to prevent creating an array that would cause me to use more memory than available RAM.

A few years ago development added the ability to limit array size as a preference, but that is a setting and is not something that can be used on the fly in code.

If you’re on Windows, you can run the memory command. If you’re not on Windows or need to write cross-platform code, then Sviatoslav’s function is for you. It gives the same outputs, in the same units, on all three supported platforms.

fn_structdisp(memoryInfo)
:
         total: 16612188
          free: 7360540
          used: 9251648
         cache: 0
    usedMatlab: 6529128
          swap: [1×1 struct]
.total:
    16612188
.free:
     7360540
.used:
     9251648
.cache:
   0
.usedMatlab:
     6529128
.swap:
         total: 1.3744e+11
          free: -2288104
          used: 1.3744e+11
    usedMatlab: 0
.swap.total:
   1.3744e+11
.swap.free:
    -2288104
.swap.used:
   1.3744e+11
.swap.usedMatlab:
   0
x = rand(12000);
fn_structdisp(memoryInfo)
:
         total: 16612188
          free: 6230044
          used: 10382144
         cache: 0
    usedMatlab: 7654196
          swap: [1×1 struct]
.total:
    16612188
.free:
     6230044
.used:
    10382144
.cache:
   0
.usedMatlab:
     7654196
.swap:
         total: 1.3744e+11
          free: -2286092
          used: 1.3744e+11
    usedMatlab: 0
.swap.total:
   1.3744e+11
.swap.free:
    -2286092
.swap.used:
   1.3744e+11
.swap.usedMatlab:
   0

Comments

Give it a try and let us know what you think here or leave a comment for Sviatoslav.

Published with MATLAB® R2019a

4 CommentsOldest to Newest

Yair Altman replied on : 1 of 4
Memory profiling is an integral part of any serious program development, and it's too bad that this has fallen by the wayside in Matlab. Your spotlight on Sviatoslav's memory utility and the clunky and nearly useless "memory" command just makes the existing gap all the more evident. It would be a very welcome addition to Matlab if, after countless years of being available as an undocumented feature, MathWorks would finally make the "profile -memory on" command a fully documented/supported feature. * https://undocumentedmatlab.com/blog/profiling-matlab-memory-usage * https://stackoverflow.com/questions/21997579/memory-profiling-of-matlab-columns-meaning
Eric Anderson replied on : 3 of 4
As a person who is not a professional developer, but this is part of my daily workflow... I would also like to see the size of different variables programmatically.

Add A Comment

Your email address will not be published. Required fields are marked *

Preview: hide