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

9 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.