Coder Social home page Coder Social logo

Memory Leak (Windows) about qt HOT 11 CLOSED

therecipe avatar therecipe commented on September 23, 2024
Memory Leak (Windows)

from qt.

Comments (11)

5k3105 avatar 5k3105 commented on September 23, 2024

Btw, this was also the case in last version I just didn't notice it until yesterday when eventually it crashed.

from qt.

therecipe avatar therecipe commented on September 23, 2024

Hey

Yes it's currently leaking memory, because I haven't really worked on that issue yet.

But here are a few things you can do to minimize the memory usage:

  1. Use the binary in deploy/windows_minimal/, which should also be much smaller in size.
  2. Always try to set a proper parent (if possible), when creating a new object with NewQ*().
  3. If you regularly create and discard objects, try to destroy them with DeleteLater() or DestroyQ*() once you don't need them anymore.

from qt.

5k3105 avatar 5k3105 commented on September 23, 2024

Thank you. I'll keep an eye out for these cases.

from qt.

therecipe avatar therecipe commented on September 23, 2024

I worked on the memory leaks 1693c80
This should hopefully fix most of them.

from qt.

5k3105 avatar 5k3105 commented on September 23, 2024

Wow. That's a huge amount of work! I look forward to testing it out - thank you!

from qt.

5k3105 avatar 5k3105 commented on September 23, 2024

I didn't notice any change in memory consumption. Testing on Table example, tabbing through, entering data all keep increasing memory.

from qt.

therecipe avatar therecipe commented on September 23, 2024

Hey

I made two new commits 57465bf and 2f211fc which should also reduce the memory consumption / fix memory leaks.

If I keep adding and removing 30 rows to the table example the ram usage stays around 15mb.
I also tested your graph app and the ram usage goes up to 50mb if I keep adding and removing 30 rectangles.
Tested this on Win 7 x64 with 8gb ram and go 1.7

Can you give me some example code where your app crashes because you run out of memory?

from qt.

5k3105 avatar 5k3105 commented on September 23, 2024

Looks like you updated 2 hours ago. I am commenting on the code I downloaded yesterday:

It certainly uses less memory. At one point I could see it stabilize between 25 and 30 mb.

It's crashing consistently now though after about 15 rectangles. I am pretty sure it's the same code I was using before. On the older compiles the memory would increase to over a gb in about 5 minutes but wouldn't crash as consistently as it does now.

This is code very similar to what I uploaded to github. I do not explicitly destroy objects. However, the memory increases as the mouse moves over the canvas without creating any objects.

Edit: Same behavior with new code. Once it hits 35mb it drops down to 25mb then shortly after crashes.

Adding no rectangles to scene it hangs out around 21mb. Cursoring over the canvas increases memory but it seems to garbage collect this.

Adding one rectangle and cursoring around increases memory to up to 28mb and back down to 24mb.

A third rectangle increases this shortly to 36mb then back down to 24mb.

Adding about 6 rectangles slowly, it crashes. It also crashes adding 13 rectangles quickly. It seems once it goes past the 38mb point it crashes (because it doesn't garbage collect quickly enough?). Also noticing that the more objects on the canvas, the higher the memory leak as I move the cursor. So if no rectangles, the memory increases by a few K at a time. If there are multiple rectangles on the canvas, as I move the mouse the memory increases by megabytes over the same distance.

from qt.

therecipe avatar therecipe commented on September 23, 2024

Sorry I added a few wrong runtime.SetFinalizers to some class constructors, which also included the QGaphicsItems and QTreeWidgetItem class and that probably caused the crashes.
But I fixed that with this commit c4e5523

I also looked into the graph app code again and made a few changes
https://github.com/therecipe/AM/commit/1c8d3bd4656beb0bd670c08dcfb501647f14c326

Instead of scene.RemoveItem() you should use DestroyQGraphicsItem, because this will remove the item and also destroys them instead of just removing them from the scene.

I also removed the unnecessary scene.Repaint() functions (especially in the scene.MouseMoveEvent function, which was consuming a lot of memory)

You should also always try to use scene.Update() instead of scene.Repaint() because, it is more efficient and won't force the repainting of all items.

And code that get executed a lot, like the ConnectMouseMoveEvent or ConnectPaint functions should be created carefully to avoid unnecessary garbage.

Hope it works now :)

from qt.

5k3105 avatar 5k3105 commented on September 23, 2024

Hi tr, thanks for your help. It's working much better now.

Things I noticed:

  1. Adding rectangle does not increase memory.
  2. Moving cursor around canvas still increases memory for some reason.
  3. Picking up and moving a rectangle is the largest memory hog. I have been able to go from 30mb to 80mb by moving the rectangle around the canvas. Fortunately gc kicks in after 2-5sec and it goes back down to 30mb.

I added over 50 nodes and there was no crash. Average mem use was ~30mb. Max mem use was ~80mb for a short time.

I do wonder why memory increases in the mouseMoveEvent since at this point all it uses is PrepareGeometryChange(). I commented out PrepareGeometryChange() and this did not change any of the effects on memory use. Maybe this is just a qt canvas thing. I have no experience otherwise with it.

It seems to be very stable now :) Thank you for all your hard work!

from qt.

therecipe avatar therecipe commented on September 23, 2024

Hey, thank you :)

It seems like the big part of the memory consumption comes from the *.Paint() functions.
The PrepareGeometryChange() functions just seem to call the *.BoundingRect() implementations.

I made a few changes in the *.Paint() functions and the memory consumption went down a little.
But after I removed the SetViewportUpdateMode() function, the memory consumption for the drag and drop operation halved. (From ~3mb steps to ~1.5mb steps)

So it still goes up to ~80mb and then cuts down to ~30mb, but slower.
https://github.com/therecipe/AM/commit/21416545b12ffb0d671a2fc9eafab3029722ef9e

I also tried to remove the background grid and the memory consumption for the drag and drop operations went down to ~0.5mb steps.
But it was acting glitchy, probably because of the missing calls to Update() or due the new/default ViewportUpdateMode.

So you probably could reduce the memory consumption even further if you use Scene. SetBackgroundBrush() (with the Qt::CrossPattern BrushStyle or with some custom style) and also with QGraphicsView::CacheBackground.
But I haven't tested this.

from qt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.