Qt Application Slowing Down
I'm developing a Qt application for the Beaglebone.
I have a few different screens and as I move forward and back through the different screens, the application gets slower and slower. In the end it gets very slow.
When one screen is finished, I move to the next with code like the following:-
@ Startup *j = new Startup();
Here is a link to the output of htop. https://www.dropbox.com/s/gcl23i3p12zkt8s/Htop_Output.jpg
New instances of my application _mita6 _ seem to be created when I navigate between screens. How can I prevent or delete these?
Am I doing something wrong when navigating between screens or should I be somehow deleting the old screen when I move to the next one? The application is snappy enough in the first couple of screens. How can I free up the processor/memory to make the application more responsive?
Any help is appreciated.
If you create a new screen each time you go back and forth without deleting any of them then you have a memory leak. Either create each screen once and reuse them (better) or delete them once you're done with one.
You might be interested in the QWizard design for your application
OK, I looked at QWizard but the fastest way to sort this would be to delete each screen when I'm done with it.
I've spent the day searching the forums and trying things like:-
This is executed as last thing before exit and the memory leak is still there.
What am I missing?
As SGaist said, in C++, everything that gets created must be destroyed. In other words, for every "new", there must be a "delete" so if you allocate memory, you are responsible for its release.
You can also make use of Qt's parent/child object ownership model where parents delete their children when they themselves are deleted
if you are using QWidgets, have a look at the Qt::WA_DeleteOnClose flag
Any combination of the above.
If you are unfamiliar with memory management in C++, I suggest you start reading up on that :)
Oh, and I suggest you look into keeping your screens in tact (i.e. creating once for the duration of your application's lifetime) also as per SGaist's suggestion. As a rule, dynamic memory allocation off the heap is much slower than static allocation off the stack.
To add to goblincoding, delete ui doesn't delete your widget.
Reading this again made me think of another thing: you seem to be creating each new screen from your soon to be deleted screen. You should rather have a manager that handles that for you.
You have to rearrange your application in line with some of the above posts.
I think each time you run the code you posted above, you are creating new instances of "Startup" with your "j" variable pointing to the newest instance, and the previous one getting lost.
You can also use something like:
and arrange it so that j goes out of scope (so that it is destroyed automatically) when you are done with the window. You will need some code to manage the different windows in this manner.
I replaced the above code with the following in every location I create a new screen.
@ Startup *j = new Startup();
This seems to reduce the memory consumed as shown in Htop but the application is still slowing down. Is this the correct way to do this?
As said before, recreating constantly each screen is not the best way to do it.
Has for the slowdown, without knowing what your program does that's pretty much crystal ball debugging. Do you initialize anything ? Do you have some code that might timeout etc...
I an not an expert but just thinking logically, over writing the "j" variable each time points it to the new instance of "Startup". I don't think either Qt or the Compiler will have any reference to the old instance (in this case the current window) and hence no way to free the memory used by it. I would try to find another more straight forward way to do this.