check parameter used by function with large memory
이전 댓글 표시
Hi:
I ran my code and found that after a certain time the memory is occupied by nearly 50G. I want to find the parameters which used so much memory but there are lots of sub-functions.
is there anyway to find the corresponding parameters, sub-functions, and memory used? so that I can optimize my code?
Thanks! Yu
댓글 수: 3
OCDER
2018년 7월 30일
This might be relevant for your case.
Jan
2018년 7월 30일
How do you determine the amount of "occupied" memory? Maybe all you see is the reserved memory, which does not necessarily mean, that it is in use.
Beside the parsed source code, functions do not "use" any memory at all, except if you store persistent memory. Only data occupy memory.
Without seeing the code, it is impossible to suggest an improvement.
Yu Li
2018년 7월 30일
채택된 답변
추가 답변 (2개)
Jan
2018년 7월 31일
0 개 추천
Remember, that the RAM shown in the TaskManager is not necessarily "occupied". If Matlab reserves 100MB frequently in a loop, the released memory is not necessarily given back to the operating system. And if it is, the OS might not find the time currently to overwrite it with zeros, such that it can be delivered to an application again. This means, that the TaskManager does not tell you, how much RAM the code needs. The code might run with 1GB RAM also fluently, although the memory managers of Matlab and the OS reserve more memory, when it is available.
댓글 수: 8
Yu Li
2018년 7월 31일
OCDER
2018년 8월 1일
By any chance, does your program use a custom MEX file?
Yu Li
2018년 8월 1일
OCDER
2018년 8월 1일
Ok, in that case, not a memory leak from C++... Did the memory profile at least point to the function that's using a lot of memory? You can then narrow things down and use that whos trick to figure out the large variable in that one function.
Yu Li
2018년 8월 1일
OCDER
2018년 8월 1일
If you use clear all in your matlab session, does your memory free up, or do you have to restart matlab completely to get your memory back? When you say "performance looks same like before", before what changes?
Try to search for:
- Growing global variables / matrix / cell
X = [X rand(1000, 1)];
- Accumulation of invisible figure handles with a lot of data in each
figure('Visible', 'off')
Can you show us the result of the undocumented profiler result? If all else fails, it'll probably be faster to tediously investigate each function, 1 by 1.
Yu Li
2018년 8월 1일
OCDER
2018년 8월 1일
Updated my answer above. That's a tricky bug.
I used figure ('visible','off') in my loop, but I have clear the invisible figure at end of each loop
f = figure;
clear f
No you haven't! There's a huge difference between clear and close. clear f gets rid of the variable f but the figure it points to still exist. close(f) closes the figure pointed to by f but keep f as a variable (which is now an invalid figure handle).
So it's no wonder you're low on memory, you've got all these figures open. Replace the
clear f
by
close(f);
In general, you should never use clear. clearing a variable that is created in a loop in particular serves no purpose. close on the other hand is useful.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!