[PyKDE] QWorkspace and memory

Gey-Hong Gweon gweon at umich.edu
Fri Sep 7 21:12:10 BST 2001


Dear all,

I am investigating into ways to use memory more efficiently so that my
data analysis program does not critically slow down my computer after
opening and closing hundreds of windows over time. So far, I have been
quitting and restarting my program when the slow-down occurs, but I
would feel less itchy if I could do better. I am using QWorkspace in my
program, and to get at this memory problem, I wrote the script that I
paste at the end of this message. Running this script, you get two
buttons -- "New Windows" and "Delete Windows" -- which create and delete
, respectively, 50 windows at a time. For deleting the windows, I am
just closing the windows which were created with the
Qt.WDestructiveClose widget flag. These windows are children of a
QWorkspace instance and I monitor how many children are owned by my
QWorkspace instance by looking at the .children () method. The number of
children increases (decreases) as I create (delete) windows, as
expected. However, if I look at the memory usage of this script by using
an external program "top", I get the following results.

Action	N_win	PROG SIZE (kbyte)
-----------------------
Startup	0		7836
+50		50		9052
+50		100		9844	
+50		150		10628	
+50		200		11412	
-50		150		11420	
-50		100		11420	
-50		50		11420	
-50		0		11420	
+50		50		11420	
+50		100		11420
+50		150		11420
+50		200		11516
+50		250		12328
+50		300		13104

I.e., the program size seems to only increase and never
decrease. However, notice that as N_win reaches to 200 the second time,
it uses about the same memory as the first time. Therefore, it looks as
though when windows are deleted, the program does not seem to free the
memory space for those windows, but does not hold it for nothing either
-- the space is eventually re-used if more windows are created. Is this
behavior due to Qt or PyQt? Is this the correct behavior or can I make
the program size decrease when I delete windows? If so, how?

I am using Linux Mandrake 7.2, python-2.0, and PyQt-2.3.

Thanks for reading,
Gey-Hong

===== script begins here =====
#!/usr/bin/env python

from qt import *

class PyQMainWindow (QMainWindow):
    
    def __init__ (self, parent = None, name = None, fl = 0):
        QMainWindow.__init__ (self, parent, name, fl)
        splitter = QSplitter (self)
        mdi = QWorkspace (splitter)
        self.setCentralWidget (splitter)
        splitter.setOrientation (QSplitter.Vertical)
        mdi.show ()
        b1 = QPushButton (splitter, "New")
        b1.setText ("New Windows")
        b2 = QPushButton (splitter, "Delete")
        b2.setText ("Delete Windows")
        b1.show ()
        b2.show ()
        splitter.setSizes ([100, 15, 15])
        self.connect (b1, SIGNAL ("clicked ()"), self.createWindow)
        self.connect (b2, SIGNAL ("clicked ()"), self.deleteWindow)
        self.mdi = mdi
        self.windows = []

    def closeEvent (self, ev):
        qApp.quit ()

    def createWindow (self):
        for i in range (50):
            w = QMainWindow (self.mdi, "window", Qt.WDestructiveClose)
            w.resize (50, 50)
            w.show ()
            self.windows.append (w)
        print 'Created 50 more windows.'
        print 'Mdi has', len (self.mdi.children ()), 'children.'

    def deleteWindow (self):
        if len (self.windows):
            for w in self.windows [:50]:
                w.close ()
            del self.windows [:50]
            print 'Closed 50 windows.'
            print 'Mdi has', len (self.mdi.children ()), 'children.'

def main (* args):
    app = QApplication (* args)
    win = PyQMainWindow ()
    win.resize (300, 300)
    app.setMainWidget (win)
    win.show ()
    app.exec_loop()

if __name__ == "__main__":
    main ([])
===== script ends here =====
-- 
Gey-Hong Gweon
500 E. University, Randall Lab, U. Mich., Ann Arbor, MI 48109-1120




More information about the PyQt mailing list