Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I conserve memory in MATLAB by declaring variables global instead of passing them as arguments?

I am new to MATLAB, it wasn't in the job description and I've been forced to take over for the person who wrote and maintained the code my company uses. Life's tough.

The guy from which I'm taking over told me that he declared all the big data vectors as global, to save memory. More specifically, so that when one function calls another function, he doesn't create a copy of the data when he passes it over.

Is this true? I read Strategies for Efficient Use of Memory, and it says that

When working with large data sets, be aware that MATLAB makes a temporary copy of an input variable if the called function modifies its value. This temporarily doubles the memory required to store the array, which causes MATLAB to generate an error if sufficient memory is not available.

It says something very similiar in Memory Allocation For Array #Function Arguments:

When you pass a variable to a function, you are actually passing a reference to the data that the variable represents. As long as the input data is not modified by the function being called, the variable in the calling function and the variable in the called function point to the same location in memory. If the called function modifies the value of the input data, then MATLAB makes a copy of the original array in a new location in memory, updates that copy with the modified value, and points the input variable in the called function to this new array.

So is it true that using global can be better? It seems a little sloppy to blithely declare all the large data as global, instead of making sure that none of the code modifies its input argument. Am I wrong? Does this really improve RAM usage?

like image 950
Shalom Craimer Avatar asked Aug 11 '09 06:08

Shalom Craimer


People also ask

How does MATLAB allocate memory?

When you assign a numeric or character array to a variable, MATLAB allocates a contiguous block of memory and stores the array data in that block. MATLAB also stores information about the array data, such as its class and dimensions, in a small, separate block of memory called a header.

What does global variable do in MATLAB?

Ordinarily, each MATLAB® function has its own local variables, which are separate from those of other functions and from those of the base workspace. However, if several functions all declare a particular variable name as global , then they all share a single copy of that variable.

Does MATLAB use a lot of memory?

When Matlab is idle it uses at least 40% of the computers memory. If I try to do anything the memory usage goes up to 80%/90%.


1 Answers

In my experience, provided that none of the code modifies the large data, memory usage is the same, regardless of whether you use a global variable or an input argument, just like the Matlab docs say. Further information is in this blog post by a MathWorks employee.

There is quite a bit of folklore on performance issues in Matlab and not all of it is right. The internals of Matlab have changed quite a bit. It may be that in a previous version it's better to use a global variable.

like image 84
Jitse Niesen Avatar answered Oct 23 '22 16:10

Jitse Niesen