About memory manipulation/speed up program

Hi, All,
I am doing a large scale computation problem, and I have several variables, which is large scale (>100MB). I was told that MATLAB will not automatically remove the not used memory in running, is that right?
If so, can I put into the code that "clear -var" to remove it, so that speedup the program? Is there some one who has done this before? What is the effect then?
Thanks.

1 Comment

Sorry, I may not be very clear on this question. I am doing large scale optimization computation. I am wondering if I remove some unused large variable to make memory efficient, will there a chance to speedup the computing a lot?
Or does someone have done this before? Thanks.

Sign in to comment.

 Accepted Answer

There are some situations in which removing unused large variables could speed up calculations a lot, but if the variables are just sitting in memory without being touched then probably it is not significantly affecting the speed of your calculations.
What hammers speed the most in MATLAB tends to be failing to pre-allocate arrays to reasonable sizes, and then to increase the size of the array on (most or every) iteration.

6 Comments

Thanks, Walter, yes, re-allocate array costs time. I tried to avoid this happen in my code. However, some variables are very large, and MATLAB will not automatically remove them from what I was told. So if I remove it and then equip them when necessary may make the loaded memory smaller.
That is why I asked this question. Have you done this test before?
Also, my code has a lot of loops which cannot be avoided. That is another reason that kilss the program.
Smaller memory can make a difference in two possible ways:
1) If, between everything running on the computer (including the operating system), more initialized logical memory is being used than there is physical memory, then the operating system will need to "swap" active memory to and from disk.
When physical memory gets full, and more memory is requested, the operating system would choose a block of physical memory, write the contents of the block to disk, scrub the contents of the block of physical memory, and then turn over the block of physical memory to whichever program had requested the memory. When the original program needs the block of logical memory back, then the operating system would choose some block of physical memory, write it's contents to disk, scrub the physical memory, and then read the block of logical memory off of disk into the block of physical memory and turn the block of physical memory over to the program to continue.
This process of "swapping" can be very very slow compared to the case where there is enough physical memory for everything.
When there is enough physical memory for everything then it does not matter (for this purpose) whether any particular block of physical memory is being actively used or not: this mechanism does not need to kick in until there is more logical memory needed than there is physical memory to store it.
2) When memory is being allocated and deallocated as expressions get calculated and results discarded, each time a block of memory is needed for storing an array, MATLAB has to look through its list of available memory blocks (that are allocated to MATLAB but are idle) and pick a "good one" to hand out. "good" is going to depend upon the pattern of future memory use. Handing out the first available block that is at least as big as is needed can turn out to be a bad strategy. If all of the "idle" variables are cleared out, then MATLAB would have greater flexibility about where to assign new requests, which can speed up the process of finding the "best" block to allocate. So it can make a difference -- but whether it will or not is going to depend on your access patterns and the amount of memory you have available to MATLAB. My testing with my program would not give any useful information about how things would go with your program.
C Zeng
C Zeng on 14 Jun 2013
Edited: C Zeng on 14 Jun 2013
Walter, thanks for sharing this.
Yes, I agree that memory read/write is much faster than hard drive. And if all variables are stored in memory, then removing unused variables may not have any advantage.
Just in case, memory is not enough and system calls for hard drive, then swapping is time consuming!
Regarding to your second point, yes there may be chance that removing inactive big variables can help, as you said. Also I found that C++ runs faster than MATLAB generally, especially for lots of loops. Could you explain it? I was told that MATLAB allocates memory different than C++. Is that the reason?
I really appreciate it. Nice weekend!
C++ is compiled to machine language. MATLAB is an interpreter. MATLAB Compiler, despite its name, produces an intermediate form that is interpreted. MATLAB Coder processed code is compiled, but MATLAB Coder can only handle a fraction of MATLAB and libraries.
Loops have gotten much better in time in MATLAB.
MATLAB knows how to interface to numeric routines such as BLAS that are highly optimized and potentially multiprocessor (even without the Parallel Computing Toolkit); MATLAB also has some heuristics to decide when it would be faster not to use those numeric libraries. MATLAB is thus potentially quite efficient, depending on what is to be done.
You could write calls to those routines in C++, but it would probably take more work and be less clear. You get a trade-off of development time versus execution time. With fast systems these days, development time is often (but not always) the more expensive over the life-cycle of the program
Dear Walter, thanks for your comments, I did not mean to hurt Matlab, in stead I like it. I was told that C++ is more faster for large scale problem than MATLAB, and it may be so however the advantage may not be too much.
And yes you are right, the development time is much less in Matlab than C++ and that is why I prefer Matlab.
For the loop, I recently found vectorize the code can largely increase the speed in Matlab. I have not tried parallel toolkit but will do it later. Thanks for your answers!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!