App running slowly after some time
24 views (last 30 days)
Show older comments
I have an app that I built in MATLAB, a rather complex app with many complex features. I noticed that over time, the app becomes laggy and slow, with every graphical action taking more time than before. for example, after running for an hour the app is much slower than it is when you start it. if you save, close and reopen the app and reload the speed is back to normal. Is there a solution to this? or at least a way to understand why this happens?
many thanks
Nathan
0 Comments
Answers (2)
Sivsankar
on 15 Jul 2024
Hi Nathan,
Without the code, I could maybe suggest some troubleshooting techniques for you that may work.
You can avail the ‘profile’ tool present in MATLAB that could help you in understanding which function is taking much time and you can try to optimise those function. You can leverage this documentation on the tool
You could also monitor the memory usage and see if memory consumption is increasing drastically overtime using the ‘memory’ function as well. This is the documentation on the ‘memory’ function
Please try these troubleshooting techniques and observe for any room for improvement in your code, if any. I’ll also attach a MathWorks blog that helps you in improving your code
Thank you
Steven Lord
on 15 Jul 2024
As J stated, without seeing the code it's going to be difficult or perhaps impossible to offer any concrete suggestion. But from the fact that you say "with every graphical action taking more time than before." I'm guessing you add more and more lines to your plot without deleting existing ones or reusing them to change the properties. Compare these two approaches for displaying a sine curve:
f1 = figure;
ax1 = axes('Parent', f1);
axis(ax1, [0 360 -1 1])
hold(ax1, 'on');
x = 0:360;
for k = 1:length(x)
plot(ax1, x(k), sind(2*x(k)), 'bo');
end
numberOfLines = length(findall(ax1, 'Type', 'line'))
f2 = figure;
ax2 = axes('Parent', f2);
axis(ax2, [0 360 -1 1])
hold(ax2, 'on');
h = plot(ax2, x(1), sind(2*x(1)), 'bo');
for k = 2:length(x)
h.XData = [h.XData, x(k)];
h.YData = [h.YData, sind(2*x(k))];
end
numberOfLines = length(findall(ax2, 'Type', 'line'))
This isn't exactly what I suspect you're doing, but it does illustrate the general problem. The first approach creates 361 lines each of which has one point, while the second approach (which I could have implemented using animatedline) only creates 1 line. 361 lines consume more resources (OS graphics handles, memory, etc.) than 1 line.
It is possible to start the Profiler, use your application, and then tell the Profiler to display the viewer. See the "Save Profiling Results as HTML Files" example on the profile documentation page. Turn profile on before starting or interacting with your app, interact with your app, then run profile viewer after you've performed your interactions with your app.
2 Comments
Walter Roberson
on 15 Jul 2024
You might potentially want to do something like
current_obj_count = numel(findobj());
if Iteration_count ~= 1
if current_obj_count ~= previous_obj_count
fprintf(2, 'Iteration #%d changed from %d objects to %d objects\n', Iteration_count, previous_obj_count, current_obj_count);
end
previous_obj_count = current_obj_count;
end
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!