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 个评论
Jan
Jan 2022-3-7
编辑:Jan 2022-3-7
Please post the relevant part of your code. Otherwise it is impossible to guess, how to modify the code for a propper pre-allocation.
"how can I change the unit from Kb to byte" - the unit of what?
Roman Foell
Roman Foell 2022-3-7
I can not post the whole code, but I will post a extraction of it (just wait a few minutes, I will edit this post).
In the Profile Window, the columns of Peak Memory, Allocation Memory etc. are in Kb, but for a single loop over my Algorithm Kb is to big, i would need the unit bytes or set the format of Kb to long (not just two diggits after the point).

请先登录,再进行评论。

回答(1 个)

Ashutosh Thakur
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:
I hope the above information helps you in measuring the memory usage.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by