PyQt Maya memory issues

88 views
Skip to first unread message

drOne piece

unread,
May 12, 2013, 8:36:39 AM5/12/13
to python_in...@googlegroups.com
Hi everyone!

I'm trying to build a gui for an asset-browser for maya. Im using pyqt and trying to learn how models and qListviews work. Now I have come to the loading of thumbnails which is a bit troublesome.
I have something that works like this:
- A list of files, in this case just thumbnail paths.
- I then put these paths into a custom object class which stores all my data in a hierarchical tree structure.
- I then put this nodeTree into a QAbstractItemModel and using the DecorationRole to display my thumbnails in the connected qListview.

The problem is that I load these images from a network location so this process of populating the thumbnails in the listview takes awhile. I also scale my thumbnails which also takes a fair amount of time.

To speed thing up I decided to have a look at QThread.
So instead of loading and manipulating my images in the decorationRole I do so in a separate thread. I then pass the finished loaded and scaled image as a qImage into my hierarchical tree structure. As the nodeTree updates the model gets updated and my decorationRole catches my newly created qImages and displays them.

So everything works fine, the thumbnails loads quick and everything updates as expected.
The only problem I have is that Maya allocates almost 500mb of memory when all thumbnails are loaded, there are like 100 of them for now. I can live with that memory footprint but what I'm more concerned with is that it doesn't release this memory when I close my window. And if I run my script again and open a new window I get another 500mb allocated. For every time I start my application it increases the ram-usage by roughly the same amount.

How can I make maya release all objects and thumbnails from memory when I close my script? Is this a memory leak or is the this behaviour expected?

If you have any clue of whats going on or what to do it would be highly appreciated!

thanks
/Erik

dgovil

unread,
May 12, 2013, 4:35:58 PM5/12/13
to python_in...@googlegroups.com
One, once you scale the images are you writing them back down to a cache somewhere? I assume these images don't change often once they're created, so you could cache the thumbnails somewhere to speed up time and performance.

As for the memory issue, are you using the same instance of your UI class or are you instantiating it every time? It should share most of the same memory if you use the same instance.

Also, if you are creating an instance, python will keep the memory alive I believe until that instance is nulled. You can also remove widgets in the pyqt classes, to make sure Qt marks it for garbage collection too.

so if you have x= MyClass(), once you run x=None, Python will put it up for garbage collection.

Justin Israel

unread,
May 12, 2013, 4:37:54 PM5/12/13
to python_in...@googlegroups.com
I use that same method in the current asset management software I am developing.
But some of the things that are different for me... I'm not using a tree view to load the data. My thumbnails come into a paged grid view and pages are created and destroyed between queries. I also wrote my own copy of QPixmapCache to be used with QImage, and I cache only to a specific limit, to keep the page cycling fast but not hold on to too much.

Within Maya, when you close your window, are you making sure it gets destroyed on close, or are you keeping the global reference around?
Also, are you making sure to properly parent all widgets to parent widgets, and destroy ones that are holding on to your view when needed?
What type of object are you returning for your DecorationRole? QIcon or QPixmap? I think it uses QPixmapCache under the hood (just guessing), so you might try setting a reasonable limit in QPixmapCache.
> --
> You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
> To post to this group, send email to python_in...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Jay Goodman

unread,
May 13, 2013, 1:38:25 AM5/13/13
to python_in...@googlegroups.com
I ran into the same memory explosion before when I got lazy and just scaled images down in memory rather than saving off actual thumbnails to disk.

I setup an http server to host images on and used a QThreadPool as a downloader. It worked quite well, but I had call on two 3rd party libraries: httplib2 and poster.

drOne piece

unread,
May 13, 2013, 1:28:32 PM5/13/13
to python_in...@googlegroups.com
Hi dgovil!
Im not saving the thumbnails in any cache for the moment, but I might consider doing so in the future.
Im using the same instance. I set a global variable x = myClass(). And then when I close my window i run x.close(). I also tried closing the window using standard maya/pymel syntax,
if pymel.core.window('myWindow', q=1, ex=1):

pymel.core.deleteUI('myWindow')

But same result...

I will try and set my instance to None as you suggested and see if that helps.
Thanks!

drOne piece

unread,
May 13, 2013, 1:46:24 PM5/13/13
to python_in...@googlegroups.com
Hi Justin!
I never used QPixmapCache but I will have a look at it.
I just use x = myUi() and then x.close(). Is there any other way to destroy my window? I think all of my widgets are parented to my mainwindow. I used designer to build the ui and all widgets that I use. But maybe I could manually destroy my qlistview before I close the mainwindow to make sure my pixmaps will be destroyed?
I return a QtCore.QVariant(icon) where icon is a QIcon with a pixmap attached to it. The pixmap is converted from the QImage that i created in the separate thread, with this command myPix.convertFromImage(myQImage).

QPixmapCache sounds good to me I will just have to figure out how to use it.
Thanks!

drOne piece

unread,
May 13, 2013, 1:53:40 PM5/13/13
to python_in...@googlegroups.com
Hi Jay!
Sounds to me that the memory explosion could have something to do with the scaling of the images... I will try to get rid of that function now and just use prescaled thumbnails and see if that helps or if I still have a big memory hit.


thanks!

Justin Israel

unread,
May 13, 2013, 4:16:29 PM5/13/13
to python_in...@googlegroups.com

Closing the window won't destroy the actual object by default. There are window flags that can make that happen. But all you need to do is call myWindow.deleteLater() to destroy the Qt object. It will delete all of the child objects in turn. So make sure you properly set the parent attribute of child widgets. It would be a mistake to make anything except top level windows a child of the Maya window. Most of your objects should be descendants of your own window.

QPixmapCache is a Singleton with access via static methods. So you just call stuff like insert and find to set and get pixmaps from the cache. You can change the limit of the storage probably near the top of your module. Though being that Maya is a long running python session, be aware that more than one script all share the same cache.

Reply all
Reply to author
Forward
0 new messages