QList memory free

I am trying to free memory after using QList, but it seems to be working incorrectly. Here is my code:

QList<double> * myList; myList = new QList<double>; double myNumber; cout << "CP1" << endl; getchar(); // checkpoint 1 for (int i=0; i<1000000; i++) { myNumber = i; myList->append(myNumber); cout << myList->size() << endl; } cout << "CP2!" << endl; getchar(); // checkpoint 2 for (int i=999999; i>0; i--) { myList->removeLast(); cout << myList->size() << endl; } cout << "CP3!" << endl; getchar(); // checkpoint 3 delete myList; cout << "CP4!" << endl; getchar(); // checkpoint 4 

Memory usage:

  • CP1: 460k
  • CP2: 19996k
  • CP3: 19996k
  • CP4: 16088k

So it seems that when deleting items and deleting myList , most of the memory is used. I believe there is a way to handle this, but I cannot find it.

Thanks in advance for your help.

Pawel

+4
source share
2 answers

The memory manager should not free the memory allocated by your program. There is no problem in your release.

+3
source

QList is an array based list. The array is automatically expanded, but not automatically compressed. Removing items from the list does not affect the size of the array.

To crop the array to its actual size, create a new QList and add it to it. Then delete the original list.

Unfortunately, there seems to be no convenient method for this, such as List.TrimExcess () in .NET.

0
source

Source: https://habr.com/ru/post/1301460/


All Articles