Sergey Korochansky
Sergey Korochansky

Reputation: 45

QString's in QVector memory leak

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?

Upvotes: 3

Views: 999

Answers (1)

Dewfy
Dewfy

Reputation: 23624

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

Upvotes: 2

Related Questions