【问题标题】:memory-size of boost::multi_arrayboost::multi_array 的内存大小
【发布时间】:2014-11-23 20:22:27
【问题描述】:

我有一个 boost::multi_array 的 3 维

 boost::multi_array<Struct, 3>* newArr = new boost::multi_array<Struct, 3>(boost::extents[x][y][z], boost::fortran_storage_order())

有没有方法来计算 newArr 的大小,还是应该直接使用

sizeof(Struct)*x*y*z ?

它们会一样吗? (我希望 multi_array 有一点控制器数据)

【问题讨论】:

    标签: boost boost-multi-array


    【解决方案1】:

    你应该可以使用

    sizeof(Struct)*x*y*z + sizeof(boost::multi_array<Struct, 3>)
    

    让我搜索一下文档是否揭示了一种更简单/更通用的方法。

    另外,我喜欢不打扰并使用例如的输出。 valgrind --tool=massif 确切知道分配在哪里。如果你有,这也会给出相关的结果,例如

    struct Struct {
        std::string x;
        char const* my_data;
    };
    

    以下是 Massif 在使用仅带有 std::string 成员的 Struct 时的示例输出:

    #include <boost/multi_array.hpp>
    struct Struct {
        std::string x;
    };
    
    int main() {
        int x=300,y=400,z=400;
        boost::multi_array<Struct, 3>* newArr = new boost::multi_array<Struct, 3>(boost::extents[x][y][z], boost::fortran_storage_order());
    }
    

    这会导致例如在我的系统上 384,000,160 字节,这正是您添加 std::cout &lt;&lt; 300*400*400*sizeof(Struct)+sizeof(*newArr); 时打印的内容

    /tmp$ valgrind --tool=massif --detailed-freq=1 ./test
    /tmp$ ms_print massif.out.32149 
    --------------------------------------------------------------------------------
    Command:            ./test
    Massif arguments:   --detailed-freq=1
    ms_print arguments: massif.out.32149
    --------------------------------------------------------------------------------
    
    
        MB
    366.2^                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
         |                                                                       @
       0 +----------------------------------------------------------------------->Mi
         0                                                                   1.273
    
    Number of snapshots: 3
     Detailed snapshots: [0, 1, 2]
    
    --------------------------------------------------------------------------------
      n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
    --------------------------------------------------------------------------------
      0              0                0                0             0            0
    00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
    
    --------------------------------------------------------------------------------
      n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
    --------------------------------------------------------------------------------
      1      1,332,810              168              160             8            0
    95.24% (160B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
    ->95.24% (160B) 0x400ADA: main (test.cpp:8)
    
    --------------------------------------------------------------------------------
      n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
    --------------------------------------------------------------------------------
      2      1,334,836      384,004,208      384,000,160         4,048            0
    100.00% (384,000,160B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
    ->100.00% (384,000,000B) 0x401722: __gnu_cxx::new_allocator<Struct>::allocate(unsigned long, void const*) (new_allocator.h:94)
    | ->100.00% (384,000,000B) 0x40138D: boost::multi_array<Struct, 3ul, std::allocator<Struct> >::allocate_space() (multi_array.hpp:474)
    |   ->100.00% (384,000,000B) 0x400E52: boost::multi_array<Struct, 3ul, std::allocator<Struct> >::multi_array(boost::detail::multi_array::extent_gen<3ul> const&, boost::general_storage_order<3ul> const&) (multi_array.hpp:195)
    |     ->100.00% (384,000,000B) 0x400AEB: main (test.cpp:8)
    |       
    ->00.00% (160B) in 1+ places, all below ms_print's threshold (01.00%)
    

    【讨论】:

    • 哇,令人印象深刻的答案,如果可以的话,我会支持你两次。对于可能想知道大小的所有其他类型的对象也非常有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多