# Memory Info4

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

### Note

Comments are closed.

## 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
Sean de Wolski replied on : 2 of 4
I completely agree, Yair! As per usual, I'll forward the comments to dev.
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.
Sean de Wolski replied on : 4 of 4
Eric, Have you seen the whos() function?