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 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!