0

I looked into this, but not helpful to me.

I have a boost bimap where I have 1 million entries of long long int. Theoretically it takes 1000000*8*2 bytes = 16 MB of memory. But I found that total memory used is 73 MB. I don't know what is going wrong.

Following is the example code:

main.cpp

#include <iostream>
#include <map>
#include "index.h"    

template<typename T>
struct MemoryMapAllocator : std::allocator<T> {
    typedef typename std::allocator<T>::pointer pointer;
    typedef typename std::allocator<T>::size_type size_type;
    template<typename U> struct rebind { typedef MemoryMapAllocator<U> other; };

    MemoryMapAllocator() {}

    template<typename U>
    MemoryMapAllocator(const MemoryMapAllocator<U>& u) : std::allocator<T>(u) {}

    pointer allocate(size_type size, std::allocator<void>::const_pointer = 0) {
        void* p = std::malloc(size * sizeof(T));
        if(p == 0)
            throw std::bad_alloc();
        return static_cast<pointer>(p);
    }
    void deallocate(pointer p, size_type) {
        std::free(p);
    }
};

typedef std::map<void*, std::size_t, std::less<void*>,
            MemoryMapAllocator<std::pair<void* const, std::size_t>>> MemoryMap;

MemoryMap& getMemoryMap() {
    static MemoryMap memMap;
    return memMap;
}

std::size_t totalAllocatedMemory() {
    std::size_t sum = 0;
    for(auto& e : getMemoryMap())
        sum += e.second;
    return sum;
}

void* operator new(std::size_t size) {
    void* mem = std::malloc(size == 0 ? 1 : size);

    if(mem == 0)
        throw std::bad_alloc();

    getMemoryMap()[mem] = size;
    return mem;
}

void operator delete(void* mem) noexcept {
    getMemoryMap().erase(mem);
    std::free(mem);
}


int main(){
    reference_index_hash();
     std::cout << "Memory allocated: " << totalAllocatedMemory() << " bytes." << std::endl;
     return 0;
}

index.cpp

#include <iostream>
#include <boost/unordered_map.hpp>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/unordered_multiset_of.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>


namespace bimaps = boost::bimaps;
typedef boost::bimap<bimaps::unordered_set_of<unsigned long long int>,
        bimaps::unordered_multiset_of<unsigned long long int> > bimap_reference;
typedef bimap_reference::value_type position;
bimap_reference numbers;


/*
 * Creating a bimap structure.
 */

int reference_index_hash(){
    for (unsigned int i=0; i < 1000000; ++i ){
        numbers.insert(position(i, i+10));
    }
    std::cout << "bimap creation....done" << std::endl;
    return 0;
}

index.h

#ifndef INDEX_H_
#define INDEX_H_


#include<iostream>
#include <boost/unordered_map.hpp>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/unordered_multiset_of.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>

extern int reference_index_hash();

namespace bimaps = boost::bimaps;

typedef boost::bimap<bimaps::unordered_set_of<unsigned long long int>,
        bimaps::unordered_multiset_of<unsigned long long int > > bimap_reference;
typedef bimap_reference::value_type position;
extern bimap_reference reference_index_vector;



#endif /* INDEX_H_ */

libboost-all-dev info

Package: libboost-all-dev
Versions: 
1.58.0.1ubuntu1 (/var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_xenial_universe_binary-amd64_Packages) (/var/lib/dpkg/status)
 Description Language: 
                 File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_xenial_universe_binary-amd64_Packages
                  MD5: 53ae85f8e1c5428077dfe573dd683a2c
 Description Language: 
                 File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_xenial_universe_binary-i386_Packages
                  MD5: 53ae85f8e1c5428077dfe573dd683a2c
 Description Language: en
                 File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_xenial_universe_i18n_Translation-en
                  MD5: 53ae85f8e1c5428077dfe573dd683a2c


Reverse Depends: 
  libui-gxmlcpp-dev,libboost-all-dev 1.35
  libboost-all-dev:i386,libboost-all-dev
  libui-utilcpp-dev,libboost-all-dev 1.35
  games-c++-dev,libboost-all-dev
  libroboptim-core-dev,libboost-all-dev
  libpcl-dev,libboost-all-dev
  libgazebo7-dev,libboost-all-dev
  libcnoid-dev,libboost-all-dev
Dependencies: 
1.58.0.1ubuntu1 - libboost-dev (0 (null)) libboost-tools-dev (0 (null)) libboost-atomic-dev (0 (null)) libboost-chrono-dev (0 (null)) libboost-context-dev (0 (null)) libboost-coroutine-dev (0 (null)) libboost-date-time-dev (0 (null)) libboost-exception-dev (0 (null)) libboost-filesystem-dev (0 (null)) libboost-graph-dev (0 (null)) libboost-graph-parallel-dev (0 (null)) libboost-iostreams-dev (0 (null)) libboost-locale-dev (0 (null)) libboost-log-dev (0 (null)) libboost-math-dev (0 (null)) libboost-mpi-dev (0 (null)) libboost-mpi-python-dev (0 (null)) libboost-program-options-dev (0 (null)) libboost-python-dev (0 (null)) libboost-random-dev (0 (null)) libboost-regex-dev (0 (null)) libboost-serialization-dev (0 (null)) libboost-signals-dev (0 (null)) libboost-system-dev (0 (null)) libboost-test-dev (0 (null)) libboost-thread-dev (0 (null)) libboost-timer-dev (0 (null)) libboost-wave-dev (0 (null)) libboost-all-dev:i386 (32 (null)) 
Provides: 
1.58.0.1ubuntu1 - 
AwaitedOne
  • 992
  • 3
  • 19
  • 42
  • 2
    The bimap uses more than the two `long long int` variables in its internal data structure. – GWW Jul 18 '17 at 15:15
  • @GWW Even, I think it should not be this much. – AwaitedOne Jul 18 '17 at 15:25
  • 2
    It certainly has more overhead than the data you are trying to store. Did you check the memory specifications of the data structure you are using? Further more, if you don't absolutely need a bimap, you can use std::unordered_map (ggc implementation is pretty good) or google's sparse hash map implementation. – Cengiz Kandemir Jul 18 '17 at 16:03
  • Your example is ignoring the multiset nature of the right side of the relation. Can you use `bimap`? – Caleth Jul 19 '17 at 09:05
  • @Caleth Thank you. No, it should be `multiset_of`, as there are duplicates in original problem. – AwaitedOne Jul 19 '17 at 17:08

0 Answers0