Results 1 to 10 of 10

Thread: Pixmap memory consumption

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Pixmap memory consumption

    Hey there, I'm developping a Qt app.

    At some point in my application I'm loading 500 widgets and 1000 pixmap inside of it.

    Since some pictures appears 2+ times, I've created a Pixmap "pool" manager to load once and for all a given Pixmap.


    Here are the figures, compiled in static release :

    at the beginning : my app takes 10 000 K of memory.
    when 1000 Pixmap + Widgets loaded : my app takes 32 000 K of memory.
    when unloaded : my app STILL takes 32 000 K of memory (if my Vista control panel is right).


    I'm pretty sure I've deleted everything.

    I'm not sure what could eat up so much memory since my "Pixmap manager" claims to contain 10 pixmap at the end.
    Last edited by bunjee; 29th November 2007 at 12:17.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pixmap memory consumption

    Hard to say without seeing actual code. Did you explicitely call delete on every widget and pixmap? Or what do you mean by "being sure" they got deleted?

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Pixmap memory consumption

    Here is my getPixmap function

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. QPixmap & ZePixmapManager::getPixmap(QString & pixmapName,
    5. const QPixmap & pixmap,
    6. int width,
    7. int height)
    8. {
    9. ZeLog::get()->Print("ZePixmapManager - getPixmap 2 %s %d %d\n",
    10. pixmapName.toStdString().c_str(), width, height);
    11.  
    12. QPixmap tempPixmap(pixmap);
    13.  
    14. if (tempPixmap.isNull())
    15. {
    16. tempPixmap = QPixmap(PICTURE_DEFAULT);
    17. pixmapName = PICTURE_DEFAULT;
    18. }
    19.  
    20. for (unsigned int i = 0; i < mPixmapItem.size(); i++)
    21. {
    22. if (mPixmapItem[i]->getPixmapName() == pixmapName
    23. &&
    24. mPixmapItem[i]->width() == width
    25. &&
    26. mPixmapItem[i]->height() == height)
    27. {
    28. ZeLog::get()->Print("ZePixmapManager - Pixmap already exists\n");
    29.  
    30. mPixmapItem[i]->addInstance();
    31.  
    32. return mPixmapItem[i]->getPixmap();
    33. }
    34. }
    35.  
    36. ZePixmapItem * pixmapItem = new ZePixmapItem(pixmapName,
    37. tempPixmap.
    38. scaled(width, height,
    39. Qt::IgnoreAspectRatio,
    40. Qt::SmoothTransformation));
    41.  
    42. mPixmapItem.push_back(pixmapItem);
    43.  
    44. ZeLog::get()->Print("ZePixmapManager - getPixmap %d %d\n",
    45. pixmapItem->getPixmap().width(),
    46. pixmapItem->getPixmap().height());
    47.  
    48. return pixmapItem->getPixmap();
    To copy to clipboard, switch view to plain text mode 
    }

    and my delete pixmap :

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. bool ZePixmapManager::DeletePixmap(const QString & pixmapName,
    5. const QPixmap & pixmap)
    6. {
    7. ZeLog::get()->Print("ZePixmapManager - DeletePixmap %d %s %d %d\n",
    8. mPixmapItem.size(),
    9. pixmapName.toStdString().c_str(),
    10. pixmap.width(),
    11. pixmap.height());
    12.  
    13. for (unsigned int i = 0; i < mPixmapItem.size(); i++)
    14. {
    15. if (mPixmapItem[i]->getPixmapName() == pixmapName
    16. &&
    17. mPixmapItem[i]->width() == pixmap.width()
    18. &&
    19. mPixmapItem[i]->height() == pixmap.height())
    20. {
    21. ZeLog::get()->Print("ZePixmapManager - DeletePixmap instance %d\n",
    22. mPixmapItem[i]->getInstance());
    23.  
    24. mPixmapItem[i]->deleteInstance();
    25. if (mPixmapItem[i]->getInstance() == 0)
    26. {
    27. ZeLog::get()->Print("ZePixmapManager - DeletePixmap complete\n");
    28.  
    29. delete mPixmapItem[i];
    30. mPixmapItem.remove(i);
    31. }
    32.  
    33. return true;
    34. }
    35. }
    36.  
    37. return false;
    38. }
    To copy to clipboard, switch view to plain text mode 

    And here is my pixmap item :

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3. // ZePixmapItem
    4. //=============================================================================
    5. //=============================================================================
    6.  
    7. ZePixmapItem::ZePixmapItem(const QString & pixmapName,
    8. const QPixmap & pixmap) :
    9. mPixmapName(pixmapName),
    10. mPixmap(pixmap)
    11. {
    12. mWidth = mPixmap.width();
    13. mHeight = mPixmap.height();
    14.  
    15. mInstance = 0;
    16. addInstance();
    17. }
    18.  
    19. //=============================================================================
    20. //=============================================================================
    21. ZePixmapItem::~ZePixmapItem()
    22. {
    23. }
    24.  
    25. //=============================================================================
    26. //=============================================================================
    27.  
    28. void ZePixmapItem::addInstance()
    29. {
    30. mInstance++;
    31. }
    32.  
    33. //=============================================================================
    34. //=============================================================================
    35.  
    36. void ZePixmapItem::deleteInstance()
    37. {
    38. mInstance--;
    39. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pixmap memory consumption

    Quote Originally Posted by bunjee View Post
    for (unsigned int i = 0; i < mPixmapItem.size(); i++)
    {
    ...
    mPixmapItem.remove(i);
    ...
    }
    When you remove item from the list, all items after it are shifted from position n+1 to n, but you always increment i. Which means that every time you remove something, you also skip the next item.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pixmap memory consumption

    What about deleting widgets? We haven't seen any widget related code.

  6. #6
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Pixmap memory consumption

    To jacek:

    Qt Code:
    1. if (mPixmapItem[i]->getPixmapName() == pixmapName
    2. &&
    3. mPixmapItem[i]->width() == pixmap.width()
    4. &&
    5. mPixmapItem[i]->height() == pixmap.height())
    6. {
    7. ZeLog::get()->Print("ZePixmapManager - DeletePixmap instance %d\n",
    8. mPixmapItem[i]->getInstance());
    9.  
    10. mPixmapItem[i]->deleteInstance();
    11. if (mPixmapItem[i]->getInstance() == 0)
    12. {
    13. ZeLog::get()->Print("ZePixmapManager - DeletePixmap complete\n");
    14.  
    15. delete mPixmapItem[i];
    16. mPixmapItem.remove(i);
    17. }
    18.  
    19. return true;
    20. }
    To copy to clipboard, switch view to plain text mode 

    I see what you mean, even though I'm not sure that's an issue since I'm returning true right after deleting an item, so the rest of the list is never checked.
    Last edited by bunjee; 29th November 2007 at 14:31.

  7. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Pixmap memory consumption

    Quote Originally Posted by wysota View Post
    What about deleting widgets? We haven't seen any widget related code.
    Actually I'm using a settable widget class of my own and I can certify its dtor is called :

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. ZeSettableLayout::ZeSettableLayout(QWidget * parent) :
    5. QWidget(parent),
    6. mLayout(this)
    7. {
    8. mSettableLayout = 0;
    9. mWidget = 0;
    10.  
    11. getLayout().setMargin(0);
    12. getLayout().setSpacing(0);
    13.  
    14. mOldParent = NULL;
    15. }
    16.  
    17. //=============================================================================
    18. //=============================================================================
    19.  
    20. ZeSettableLayout::~ZeSettableLayout()
    21. {
    22. Clear();
    23. }
    24.  
    25. //=============================================================================
    26. //=============================================================================
    27.  
    28. void ZeSettableLayout::setLayout(QLayout & settableLayout,
    29. int stretch)
    30. {
    31. Clear();
    32.  
    33. mSettableLayout = &settableLayout;
    34.  
    35. getLayout().addLayout(mSettableLayout, stretch);
    36. }
    37.  
    38. //=============================================================================
    39. //=============================================================================
    40.  
    41. void ZeSettableLayout::setWidget(QWidget & widget,
    42. int stretch,
    43. Qt::Alignment alignment)
    44. {
    45. Clear();
    46.  
    47. mWidget = &widget;
    48.  
    49. // Saving old parent to restor it at deletion
    50. mOldParent = getWidget().parentWidget();
    51.  
    52. getLayout().addWidget(mWidget, stretch, alignment);
    53. getWidget().show();
    54. }
    55.  
    56. //=============================================================================
    57. //=============================================================================
    58.  
    59. void ZeSettableLayout::Clear()
    60. {
    61. if (mSettableLayout)
    62. {
    63. getLayout().removeItem(mSettableLayout);
    64.  
    65. mSettableLayout = NULL;
    66. }
    67.  
    68. if (mWidget)
    69. {
    70. getLayout().removeWidget(mWidget);
    71.  
    72. // Retoring old parent for deletion
    73. getWidget().setParent(mOldParent);
    74. mOldParent = NULL;
    75.  
    76. //getWidget().hide();
    77. mWidget = NULL;
    78. }
    79.  
    80. update();
    81. }
    To copy to clipboard, switch view to plain text mode 

    I have to reset Widget's parent everytime I set another one, otherwise its still parented to my settable layout class.

    Maybe doing so prevent QT from freeing anything ? even when calling delete?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pixmap memory consumption

    But do you call delete anywhere?

  9. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Pixmap memory consumption

    to wysota,

    Yes here :

    Qt Code:
    1. //=============================================================================
    2. //=============================================================================
    3.  
    4. void ZeServiceContactList::clearContact()
    5. {
    6. ZeLog::get()->Print("ZeServiceContactList - Clearing contacts\n");
    7.  
    8. for (unsigned int i = 0; i < mContactVector.size(); i++)
    9. {
    10. mOfflineExpand.RemoveWidget(*(mContactVector[i]));
    11. emit removeWidget(*(mContactVector[i]));
    12.  
    13. ZeQuickViewController::get()->getDynamicInterface().
    14. deleteMatch(mContactVector[i]->getStatusBubble());
    15.  
    16. ZeQuickViewController::get()->getDynamicInterface().
    17. deleteMatch(mContactVector[i]->getPictureWidget());
    18.  
    19. // Deleting widgets...
    20.  
    21. delete mContactVector[i];
    22. delete mContactPopVector[i];
    23. delete mContactPictureVector[i];
    24.  
    25. // Deleting widgets...
    26. }
    27. mContactVector.clear();
    28. mContactPopVector.clear();
    29. mContactPictureVector.clear();
    30. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Pixmap memory consumption

    The problem could be completely elsewhere. Actually, not a problem. If Vista has a good memory allocator, it won't deallocate freed memory in case the application wants it back later. You could try running another application that is memory exhaustive and see if the amount of memory allocated for the first app starts decreasing.

Similar Threads

  1. Tracking memory consumption of plugins.
    By spud in forum General Programming
    Replies: 3
    Last Post: 7th September 2007, 13:14
  2. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.