[Development] Releasing memory after clear() in Qt containers (Qt 5.X)

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Fri Apr 23 19:39:41 CEST 2021


Hi,

On 23/04/2021 19:00, Francesc Martinez wrote:
> I'm doing some research in GitQlient and I've noticed that the RAM 
> memory used doesn't decrease at all when it's supposed to. In there I'm 
> managing an internal cache (needs some improves, I know) that stores the 
> commit information in a QHash<QString, CommitInfo> 
> <https://github.com/francescmm/GitQlient/blob/master/src/cache/GitCache.h> 
> (ignore the QVector<CommitInfo*> since it just stores the references to 
> the QHash[sha] for optimization).
> 
> I fill the hash on load and clear it when I close the repo (already 
> ensure about it with logs and debugging). And the confusing thing is 
> that the memory doesn't decrease at all. So, if I keep opening and 
> closing the same repo, the memory used keeps increasing.

A couple of notes.

1) clear() on a container doesn't (necessarily) release all the memory 
held by that container. For instance std::vector / Q5Vector retain the 
allocated capacity:

> https://github.com/qt/qtbase/blob/5.15/src/corelib/tools/qvector.h#L440

A Q5Hash will release the memory held by the objects in the hash (as 
they're individually allocated) but may or may not (need to check the 
code) shrink the bucket array.

If you want to truly release all the memory used by a container, then either
*) use shrink_to_fit / squeeze after clear,
*) or move-assign an empty container,
*) or swap with an empty container.


2) How are you measuring memory usage precisely? Looking at 'top' or 
'Task manager' is the wrong way, you need a memory profiler (like 
heaptrack on Linux).


3) Unrelated to your problem, your code works in Qt 5, but in Qt 6 if 
the QHash gets modified in any way that'll invalidate your entire 
QVector you're keeping as a cache. If you want to future proof your code 
you'll need to redesign that.


HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.qt-project.org/pipermail/development/attachments/20210423/fabab1b3/attachment.bin>


More information about the Development mailing list