Boost bimap占用大量内存

时间:2017-07-18 15:09:14

标签: c++ memory boost-bimap

我调查了this,但对我没有帮助。

我有一个boost bimap我有100万条long long int条目。从理论上讲,它需要1000000 * 8 * 2字节= 16 MB的内存。但我发现使用的总内存为73 MB。我不知道出了什么问题。

以下是示例代码:

的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信息

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 - 

0 个答案:

没有答案