简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM