How to keep track of the allocated memory of a list of lists?

296 Views Asked by At

I'm trying to find out how much memory (in Bytes) a list of lists is using.

The Problem is illustrated here:

enter image description here

It Shows a Container List storing pointers to other lists (the Storage Lists) each storing uniformly size elements (let's say 10 Bytes each). So the first Storage List is storing 4 Elements, next 30, then the next one 12 and 10 and the last one 5 Elements. All the Elements are of the same C++ Type (which is just a struct of plain old data).

My Aim: The Container List above should have current size variable, which keeps track of the number of bytes the lists he is pointing to (with all tracked space for *prev and *next pointers).

So far, what I tried, is using allocators to track the number of bytes allocated by the lists (through a static class variable). Although that leads to a problem with std::list::splice, when moving elements from list see here: The elements which are spliced to another Container List are not tracked. It should even be able to track the bytes transfered to add that bytes-Count to the current size of another Container List.

  • Do I have to avoid using the STD allocators for memory tracking and instead use boost's intrusive lists somehow for tracking memory like that?

  • What kind of C++ feature does possibly support me by solving my
    problem?

1

There are 1 best solutions below

0
On

I found out that you can use EASTL allocators, because they are bound on each list object.

So on EASTL::CustomAllocator::allocate and EASTL::CustomAllocator::deallocate you can notify the main list that the allocated bytes of the sub-list (here: Storage List) has changed their weight (here: size in number of bytes allocated by Storage List).

This way the main list (here: Container List) always get a updated weight, which is cached and bound to the main list.

For the CustomAllocator which should notify the main list see my answer on:

How to track memory usage using EASTL?