How to reduce the memory consumption of unordered_multiset?

420 Views Asked by At

I used unordered_multiset in my code by the following two reasons,

  1. Should be easy to find or look up the data.
  2. Should supports to load duplicate values.

unordered_multiset are typically much faster than multisets & vector, both for insertion and for lookup, and sometimes even for deletion.

But the bad thing is,it takes too much of memory.

I have stored unsigned __int64 (8 bytes)values in the unordered_multiset and properly clear the values from the unordered_multiset. can you anyone explain,why its taking the memory & how to solve this memory consumption?

2

There are 2 best solutions below

0
On

You can get a much better measurement of how much space a std::container uses by providing it with a custom allocator that logs how much it is asked to allocate.

e.g.

std::size_t total_allocation = 0;

template< class T >
struct logging_allocator
{   
    using value_type = T;
    using pointer = T*;
    using const_pointer = const T*;
    using reference = T&;
    using const_reference = const T&;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using propagate_on_container_move_assignment = std::true_type;
    template< class U > struct rebind { using other = logging_allocator<U>; };
    using is_always_equal = std::true_type;

    pointer address( reference x ) const { return base.address(x); }
    const_pointer address( const_reference x ) const{ return base.address(x); }

    pointer allocate( size_type n, const_pointer hint = nullptr ){ 
        total_allocation += n; 
        return base.allocate(n, hint); 
    }
    pointer allocate( size_type n, const void * hint){ 
        total_allocation += n; 
        return base.allocate(n, hint); 
    }
    pointer allocate( size_type n ){ 
        total_allocation += n; 
        return base.allocate(n, hint); 
    }

    void deallocate( T* p, size_type n ) {
        total_allocation -= n; 
        return base.deallocate(p, n);
    }

    size_type max_size() const {
        return base.max_size();
    }

    void construct( pointer p, const_reference val ) {
        total_allocation += sizeof(T); 
        return base.construct(p, val);
    }
    template< class U, class... Args >
    void construct( U* p, Args&&... args ) {
        total_allocation += sizeof(U); 
        return base.construct(p, args...);
    }

    void destroy( pointer p ) {
        total_allocation -= sizeof(T); 
        return base.destroy(p);
    }
    template< class U >
    void destroy( U* p ) {
        total_allocation -= sizeof(U); 
        return base.destroy(p);
    }

private:
    std::allocator<T> base;
}
0
On

Caleth has a good suggestion, alternatively you can look at memory usage within processes

just before you insert into the multiset and after.

Most likely course of difference, a huge dll is loaded.