App running slowly after some time

11 views (last 30 days)
Nathan Bblanc
Nathan Bblanc on 15 Jul 2024
Commented: Walter Roberson on 15 Jul 2024
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

Answers (2)

Sivsankar
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
  1 Comment
Nathan Bblanc
Nathan Bblanc on 15 Jul 2024
Edited: Walter Roberson on 15 Jul 2024
Thank you for your answer J. This is not a function, this is a MATLAB application, I don't think there is a way to use the profiler while running a MATLAB application. I believe the problem is not with the MATLAB code but with something relating to the GUI.
If you want you can try downloading the app from here and playing around

Sign in to comment.


Steven Lord
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'))
numberOfLines = 361
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'))
numberOfLines = 1
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
Nathan Bblanc
Nathan Bblanc on 15 Jul 2024
Thank you Steven, This is definetly not directly the issue, plotting is a smal part of the app, and the plots have a consistent number of lines. However, it is possible that I am doing something similar- creating multiple graphical objects that clogg the memory. Is there a way to check all the graphical objects in existence at a certain point? is there something like a "cache" that I can clear?
the app and source code are available here, I can also attach them if this helps.
regarding the profiler, even if I use it I don't think I will be able to understand much. (I use the profiler a lot. in general) most of the runtime is in graphical actions which tend to be indented inside other graphical actions. I think a different approach is needed here due to the interactivity of the app.
Walter Roberson
Walter Roberson on 15 Jul 2024
Generally speaking, findobj can be used to locate graphics objects.
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

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!