Profiling Memory Peaks of code execution
조회 수: 33 (최근 30일)
이전 댓글 표시
Hi,
I was using the profiler to get acess to memory peaks in my code execution.
I am now wondering how a simple indexing command of a logical array can have peaks up to 8000Kb.
The command ist actually something like:
variable(index) = true;
Here "variable" is a logical array of size 241, so actually 241 bits (I know this has to be multiplied by 8 because Matlab stores each bit in one byte because of performance), and index is int16 between 1 and 241. And in the Code I change the value of the variable to true or false, depending on a condition.
Nevertheless this line has peaks up to 8000 Kb.
How does this come?
EDIT:
Ok, somehow the number of calls plays a role... around 100000 at the moment.
How to get the allocation memory of the single execution?
Holly ... how can I change the unit from Kb to byte or to expand the format to long or something like this?
Thanks
댓글 수: 2
답변 (1개)
Ashutosh Thakur
2024년 1월 24일
편집: Ashutosh Thakur
2024년 1월 24일
Hi Roman,
The MATLAB profiler provides a way to measure time which is spent on executing each line of code, but to measure the memory usage memory function can be used to analyze the memory usage of each line of code.Following documentation link can be reffered for more information regarding the memory function: https://www.mathworks.com/help/matlab/ref/memory.html
The following code example shows the way in which memory usage can be measured for the particular line of code:
[userview_1, ~] = memory;
variable(index) = true;
[userview_2, ~] = memory;
memory_difference = userview_2.MemUsedMATLAB - userview_2.MemUsedMATLAB;
The memory_difference variable will provide you with the value of the memory which is used by the particular line of code.
In order to resolve the memory usage issues, kindly follow the strategies mentioned below:
- Try to use appropriate data structure for the variables, classes depending on their usage so that less memory is used.
- Try to use the Sparse matrix as much as possible.
- Prevent creating the temporary copies of an array, try to pre-allocate and use repmat function as much as possible.The usage of repmat can be understand through the following documentation link: https://www.mathworks.com/help/matlab/ref/repmat.html
Please refer to the following documentation links regarding optimizing the memory usage in MATLAB:
- https://www.mathworks.com/help/matlab/matlab_prog/strategies-for-efficient-use-of-memory.html
- https://www.mathworks.com/help/matlab/matlab_prog/memory-allocation.html
- https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
I hope the above information helps you in measuring the memory usage.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!