Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QString's in QVector memory leak

Tags:

c++

qt

qstring

Why when I fill the QVector as follows:

QVector< QPair<qint32, QString> > vector(10000000);
QString temp;
for (int i = 0; i < 10000000; ++i)
{
   temp = QString::fromUtf8("Vasya");
   vector.replace(i, qMakePair(i, temp));
}

my program uses 470 MB of RAM, and when this:

QVector< QPair<qint32, QString> > vector(10000000);
QString temp2 = "Vasya";
for (int i = 0; i < 10000000; ++i)
{
    vector.replace(i, qMakePair(i, temp2));
}

it is only 90 MB of RAM?

like image 283
Sergey Korochansky Avatar asked Jul 10 '26 09:07

Sergey Korochansky


1 Answers

Because internally QString is optimized to share memory of const objects. First case needs allocate memory each time when fromUtf8 invoked. On opposite second case always can reuse memory from existing const temp2

like image 122
Dewfy Avatar answered Jul 12 '26 08:07

Dewfy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!