shizzle
shizzle

Reputation: 183

Matlab - Memory usage of a program

I'm currently implementing different signal processing algorithms in MATLAB, to later implement one of these in C++. To choose between these I'm going to perform a number of tests, one being a memory usage check. That is, I want to see how much memory the different algorithms use. Since the implementations are divided in to sub-function, I'm having problems collecting information about the actual memory usage.

This is what I've tried so far:

  1. I've used the profiler to check memory usage of every function. Problem: It only shows allocated memory usage. It doesn't show e.g. memory usage of variables in every function.

  2. I've used whos at the end of every function to collect information about all the variables in the workspace of the functions. I then added these to a global variable. Problem: The global variable keeps increasing even after the execution is done and it seems to never stop.

Now to my question. How can I, in a rather simple way, get information about the memory usage of my program, all functions included?

Best regards

Upvotes: 1

Views: 5965

Answers (3)

Marco
Marco

Reputation: 21

The best way to monitor memory usage is to use the profiler, with the memory option turned on:

profile -memory on % run your code profreport

The profiler returns memory usage and function calls statistics. Note that the memory option has an impact on your execution speed.

Upvotes: 2

Floris
Floris

Reputation: 46375

I think your strategy to call whos at the end of every function (just before it returns) is a good one; but maybe you want to print the result to the screen rather than a global. If it "keeps increasing", then maybe you have a callback function that is being called unbeknownst to you, and that includes one of your whos calls. By printing to screen (and maybe including a disp('**** memory usage at the end of <function name> ***') just before it, you will find out why it "keeps going".

The alternative of using memory is somewhat helpful, but it gives information about "available" memory, as well as all the memory used by Matlab (not just the variables).

Of course any snapshot of memory usage doesn't necessarily grab the peak - it's possible that a statement like

x = sum(repmat(A, [1000 1]));

would require quite a large peak memory usage (as you replicate the matrix A 1000 times), yet a snapshot of memory (or running whos) right before or after won't tell you what just happened...

Upvotes: 2

Mihai8
Mihai8

Reputation: 3147

You can use memory function. Also, see memory management functions. Take a look to matlab memory usage.

Upvotes: 0

Related Questions