Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Slow performance and large memory usage with QTableView in Qt 5.15



  • #ifndef TESTMODEL_H
    #define TESTMODEL_H
    #include <QAbstractTableModel>
    
    class TestModel : public QAbstractTableModel
    {
        Q_OBJECT
    public:
        explicit TestModel(QObject *parent = nullptr);
    
        virtual int rowCount(const QModelIndex & parent) const;
        virtual int columnCount(const QModelIndex &parent) const;
    
        virtual QVariant data(const QModelIndex &index, int role) const;
        virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    
        bool addData(int i);
    
    private:
        QVector<int> m_list;
    };
    
    #endif // TESTMODEL_H
    
    
    #include "testmodel.h"
    
    TestModel::TestModel(QObject *parent)
    {
    
    }
    
    int TestModel::rowCount(const QModelIndex &parent) const
    {
        return m_list.size();
    }
    
    int TestModel::columnCount(const QModelIndex &parent) const
    {
        return 100;
    }
    
    QVariant TestModel::data(const QModelIndex &index, int role) const
    {
        return QVariant();
    }
    
    QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if (orientation == Qt::Vertical) return QString::number(section);
        if (role != Qt::DisplayRole) return QVariant();
        return QString::number(section);
    }
    
    bool TestModel::addData(int i)
    {
        m_list.push_back(i);
        return true;
    }
    
    
    #include <QApplication>
    
    #include "testmodel.h"
    #include <QTableView>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        TestModel model;
        for(int i = 0; i < 100000; ++i)
        {
            model.addData(i);
        }
        QTableView view;
        view.setModel(&model);
        view.show();
        return a.exec();
    }
    
    

    Minimum code is provided above, I'm using QTableView and a very simple model;
    It need like 20+ secs to loaded and use over 6000MB memory, and the selection of the rows is slow too.
    I was wondering if I'm using wrong with model-view, or what can I do to improve the performace, thx



  • @KaoN
    Glancing, I think your code is OK for a test.

    You are creating 100,000 rows in your model. That in itself is OK (check the timing on just that). You are then asking QTableView to display all 100,000 rows, and at 100 columns per row. That's 10 million cells. That is not reasonable (IMO)! In a real-world scenario we would expect the model to contain less rows/columns, or we would expect you to use a QSortFilterProxyModel to filter how much is shown to the user.

    Or, if you really insist on displaying that much data, people would implement a "paging" mechanism on the QTableView so that it only shows, say, 100 rows at a time, and the user has to move forward/backward by 100 row-pages. So that the view only actually displays 100 rows at a time. You will have seen that in lots of applications.



  • @JonB Thank you for your suggest, but after several tries, I thought I might find a bug with Qt 5.15 at least.
    Same code works fine with 5.9.9, fast and smooth, but fail to perform the same when I using the latest release, with memory usage nor CPU consumption.

    Should I open a bug for that ?


  • Lifetime Qt Champion

    Your testapp opens instantly here on linux with 5.15.0 / 64 bit



  • @Christian-Ehrlicher thx for testing it for me
    , then it might be a bug related to windows only?


  • Lifetime Qt Champion

    I also don't have problems with your example with Qt5.15.0 / MinGW64 on Windows 7. Please make sure that you have a clean build dir, maybe even create a new clean project to test it.



  • @Christian-Ehrlicher hmm..that's wierd, I did a clean project and using both MSVC and MinGW64 , same bad performance on Windows 10 1909, fine with 5.9.9



  • After rebooting my pc.. everything works fine....
    still cant get why...


  • Lifetime Qt Champion

    @KaoN Then please mark this topic as solved



  • @Christian-Ehrlicher done, but can't figure out what made that 'bug' happen



  • @KaoN Windows



  • After hibernating my desktop for a night, it slows again.. and...reboot does not work anymore..TT



  • I deploy two version of the testapp to another laptop, win10 20H2, 5.9 perform good and 5.15 with no luck


  • Moderators

    @KaoN said in Slow performance and large memory usage with QTableView in Qt 5.15:

    After hibernating my desktop for a night, it slows again.. and...reboot does not work anymore..TT

    Are you running out of RAM? I'm guessing that the slowdown happens when your PC is busy transferring data between your physical RAM and your Pagefile. That is a very slow process.

    Run Resource Monitor (https://www.thewindowsclub.com/use-resource-monitor-windows-10 ) and see what is using up all your memory and your disk I/O.



  • @JKSH Thank you for your reply, but my desktop has 64GB memory, I don't think it cause the problem.
    The question is with 5.9.9, it works fine, with only 10MB memory consumption.


  • Moderators

    @KaoN said in Slow performance and large memory usage with QTableView in Qt 5.15:

    The question is with 5.9.9, it works fine, with only 10MB memory consumption.

    It's possible that the implementation of QTableView changed between Qt 5.9 and Qt 5.15 which provides better performance in some use-cases but worse performance in other use-cases.

    Do you have Qt 5.12 LTS installed? How does it perform with that version?

    You can post your minimal test code and provide instructions on how to cause the slowdown to https://bugreports.qt.io/. However, it is still not clear to me whether it is a bug/regression or not.



  • @JKSH Yes, I tried 5.12.7, slow too.
    Bug is reported with minimal code: https://bugreports.qt.io/browse/QTBUG-86069?filter=16824



  • @JKSH The weird thing is that I did get good performance after I reboot my desktop, but it did not always working..actually, it only works once.


  • Lifetime Qt Champion

    I'm pretty sure your Qt example has nothing to do with this.


  • Moderators

    @KaoN have you done a full hard shutdown or only the soft one that comes when opening the start menu ?

    hit win + r
    
    then type:
    shutdown /s /f /t 0
    


  • @J-Hilk It did help to get everything work well again...but could you plz explain a bit, is it a system bug?


  • Moderators

    @KaoN Microsoft made this change a while ago when the UWP, Windows for mobile/tablet was a thing and windows required faster reboots/boots/wakeups

    As a result all modern Windows system have a "soft reboot /shutdown" these days

    Usually you don't notice this, until some day, fundamental stuff starts behaving strangely.

    I for once managed it (with Qt no less) to get my bluetooth driver in an undefined state, so nothing, bluetooth worked anymore, and a reboot did not fix it, because the normal reboot does not reinitialize and clear everything. That's how I learned about it.

    Your case, smells like a bug hard to tell if Qt is at fault or not.
    Have you tried updating your windows ? My Bluetooth story fixed itself with a windows update.

    And have you tried it on a 2nd, different windows pc ?

    If yes, and behavior is the same (in both cases) than it may be a Qt issue



  • @J-Hilk Thanks for the information. Generally I find one case that if the application run through windows remote desktop, it will slow, even it was good before, and it will not recovery until hard reboot.
    But another my laptop perform badly what ever I reboot or hard shutdown.
    Only 5.9 works perfectly on every situation.


  • Moderators

    @KaoN
    mmh the last Idea I have, would be updating your graphics drivers, those are most often an issue. After that, bring it to https://bugreports.qt.io


Log in to reply