I'm trying to optimize memory usage in my code.
I found that some Qt containers have capacity(), reserve() and squeeze() functions for this purpose.
Making some tests I found a strange result (among normal ones!):
in some conditions I get capacity() < size().
If I understand correctly the documentation, capacity() should give the number of allocated items (which generally is greater than the number of inserted items, for speed reasons).
QHash< DataUniqueId, DrawItem* > m_lookupTable;
// Insert data into m_lookupTable
for (...) {
m_lookupTable.insert(..., ...);
}
qDebug() << m_lookupTable.size() << m_lookupTable.capacity();
// No more insertions, so release any unused memory
m_lookupTable.squeeze();
qDebug() << m_lookupTable.size() << m_lookupTable.capacity();
QHash< DataUniqueId, DrawItem* > m_lookupTable;
// Insert data into m_lookupTable
for (...) {
m_lookupTable.insert(..., ...);
}
qDebug() << m_lookupTable.size() << m_lookupTable.capacity();
// No more insertions, so release any unused memory
m_lookupTable.squeeze();
qDebug() << m_lookupTable.size() << m_lookupTable.capacity();
To copy to clipboard, switch view to plain text mode
This is the output:
500 521
500 257
500 521
500 257
To copy to clipboard, switch view to plain text mode
As you can see, after the squeeze(), size() > capacity().
How can the number of allocated items be less than the number of real items?
Am I doing something wrong?
Thanks in advance,
Alessandro
Bookmarks