How to find memory usage in App Designer
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
1 comentario
Keith Myescough
el 23 de Abr. de 2023
Could it be that this: https://nl.mathworks.com/matlabcentral/answers/1635680-possible-memory-leak-when-opening-figures-that-are-maximized-2021b is relevant for you?
Respuestas (1)
Arun
el 6 de En. de 2024
Editada: Arun
el 6 de En. de 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:
- 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:
- 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.
- 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.
- 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.
- Analyse Callbacks and Listeners: Callbacks that are not properly unregistered or event listeners that remain active can prevent objects from being cleared form memory.
- 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.
- 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.
0 comentarios
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!