Qt QString gets a lot of memory
After executing the code below, I see that the line
encoded = QString(DicResourceByteArray.toBase64().data());
gets too much RAM. Approximately 60MB.
How can I release it from memory?
Dic_block DicBlock;
qint64 Count;
int pos, len;
QByteArray DicResourceByteArray;
QDataStream out(&DicResourceByteArray, QIODevice::WriteOnly);
QString encoded;
while(DicInstance.readBlock(DicBlock))
{
if(DicBlock.type == 2)
{
pos = 0;
len = (unsigned char)DicBlock.data[pos++];
std::string filename( DicBlock.data+pos, len );
pos += len;
out.writeRawData(DicBlock.data + pos, DicBlock.length - pos);
encoded = QString(DicResourceByteArray.toBase64().data());
QString strQuery = QString("INSERT INTO Dictionary_Resources([FileName], [ImageBasedO开发者_如何转开发n64]) values('%1', '%2')").arg(QString::fromStdString(filename), encoded);
query->exec(strQuery);
delete encoded;
}
}
delete query;
db.close();
//...
DicInstance.close();
First thing: grab the data array with .toBase64().constData()
, this avoids a possible copy of your data.
Second thing: Move the declaration of QString encoded;
into the if
-block, this ensures, that after the if-block memory gets released.
Third thing: remove the delete encoded;
! (Astonishing that it compiles as encoded is not a pointer).
You don't need delete encoded
, the QString is going to be automatically deleted (and released from memory) at the end of your block.
You're doing things very inefficiently by copying the contents of encoded
into strQuery
. Bind it as a value in your query instead.
精彩评论