How to find memory usage in App Designer

8 views (last 30 days)
David L
David L on 16 Aug 2022
Edited: Arun on 6 Jan 2024
Hi, I have an AppDesigner GUI that loads new images every-time I click on a 'Next' button. (It's a panel of 25 separate 224x224x3 UI.Images, and I load in a new CData image into ImageSource for each of the 25 images on each iteration). Every iteration the memory used by Matlab grows, but I am replacing the image data on each iteration. No new data should be created each time. How do I find the source of the memory leak? Here's what I get when I assay the memory on each iteration:
user =
struct with fields:
MaxPossibleArrayBytes: 3.8748e+10
MemAvailableAllArrays: 3.8748e+10
MemUsedMATLAB: 3.2124e+09
user =
struct with fields:
MaxPossibleArrayBytes: 3.8464e+10
MemAvailableAllArrays: 3.8464e+10
MemUsedMATLAB: 3.2687e+09
user =
struct with fields:
MaxPossibleArrayBytes: 3.8371e+10
MemAvailableAllArrays: 3.8371e+10
MemUsedMATLAB: 3.3251e+09

Answers (1)

Arun
Arun on 6 Jan 2024
Edited: Arun on 6 Jan 2024
Hey David,
I understand that you can observe that the memory utilization by MATLAB increases with each iteration of your App Designer GUI execution.
Here are few techniques that you can apply to find the cause of memory leak:
  1. Use MATLAB Profiler
  • To start profiling your app, use: >>profile on -memory, on the command line.
  • Then, run your app and interact with it to trigger the potential memory leak.
  • Once done, stop the profiler and view the report using: >>profile off; >>profile viewer;
  • The Profiler report will show you which functions are using the most memory and may help you pinpoint where memory is not being released properly.
2. Monitor Memory Usage over time
  • You can use the below code snippet to monitor memory usage over time while you interact with your app. This can help you identify if the memory usage is continuously increasing, which is a sign of memory leak:
% Run this script in the MATLAB command window while interacting with your app
initialMemory = memory;
disp(['Initial Memory Usage: ', num2str(initialMemory.MemUsedMATLAB)]);
for i = 1:100 % Adjust the number of iterations as needed
pause(1); % Adjust the pause time as needed
currentMemory = memory;
disp(['Memory Usage at iteration ', num2str(i), ': ', num2str(currentMemory.MemUsedMATLAB)]);
end
Additionally, to avoid a memory leak, you can take care of some of the following things:
  1. Check for Unclosed Resources: Ensure that any resource your app opens (such as files, databases, or hardware connections) are properly closed when no longer needed.
  2. Use “clear functions: If you are creating large temporary variables within functions or callbacks, make sure to clear them once they are no longer needed.
  3. Check for large global variables: Global variables or app properties that store large amounts of data can lead to memory leaks if not managed properly.
  4. Analyse Callbacks and Listeners: Callbacks that are not properly unregistered or event listeners that remain active can prevent objects from being cleared form memory.
  5. Inspect Handle Objects: If your app uses handle objects (e.g., GUI components, timers, or custom classes that inherit from handle), make sure you are deleting them properly when they are no longer needed.
  6. Update MATLAB: Memory leaks can sometimes be the results of bugs that are fixed in later releases.
Here are some for more information related to memory leak please refer the below MATLAB documentation link that might be useful:
I hope this helps.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!