QPainter::drawText consumes a lot of memory



  • Hi,

    with this code, my program consumes an additional 100MB of Memory on a Windows 7 x64 - MSVC10 computer using Qt 4.7.4, 4.8.1 and 4.8.2.

    Is there any way to reduce or disable the caching that happens?

    @void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter *p = new QPainter(this);

    for (int s=5; s<500; s++)
    {
        QFont *f = new QFont(p->font());
        f->setPixelSize(s);
        p->setFont(*f);
        delete f;
        p->drawText(0,this->height(),"Hello World");
    }
    
    delete p;
    

    }@



  • I don't think caching is the problem; you memory allocation strategy and your code is.

    @
    void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter p(this);
    QFont f(p.font());

    int fHeight = height();
     
    for (int s=5; s<500; s++)
    {
        f.setPixelSize(s);
        p.drawText(0, fHeight, "Hello World");
    }
    

    }
    @



  • No, i am deleting all my allocated objects. I have tried your code and that has the problem that the font is not getting increased for the painter. I have corrected your code and then the memory usage is back as described above already.

    @void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter p(this);
    QFont f(p.font());

    int fHeight = height();
    
    for (int s=5; s<500; s++)
    {
        f.setPixelSize(s);
        p.setFont(f);
        p.drawText(0, fHeight, "Hello World");
    }
    

    }@

    [quote author="Lukas Geyer" date="1341817685"]I don't think caching is the problem; you memory allocation strategy and your code is.[/quote]



  • I just found out that Qt does some sort of garbage collection from time to time in the font cache. This is good and explains that there is no memory leakage.

    But is there a way to limit this cache in size?



  • I don't think there is a public API to manipulate the font cache, but probably there should be one. Feel free to file a "bug report":http://qt-project.org/doc/qt-5.0/bughowto.html.

    Is this a real world example or just some theortical code?



  • I have created a "bug report":https://bugreports.qt-project.org/browse/QTBUG-26463 and got already a constructive answer which solves the issue.

    @void MyWidget::paintEvent(QPaintEvent *event)
    {
    QPainter p(this);
    QFont f(p.font());

    int fHeight = height();

    for (int s=5; s<500; s++)
    {
    f.setPixelSize(s);
    p.setFont(f);
    p.drawText(0, fHeight, "Hello World");
    }
    QFont::cleanup();
    }
    @



  • Fine. ;-)



  • Thanks for your input.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.