gpt4 book ai didi

c++ - 我的对象消耗了多少内存

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:56:36 28 4
gpt4 key购买 nike

我想知道以编程方式获取我的用户定义类消耗的内存。以下是类的声明

struct TrieNode {
typedef std::map<char, TrieNode *> ChildType;
std::string m_word;
bool m_visited;
}

我在这个 Trie 中插入了大约 264061 个单词。在此之后,当我执行 sizeof(trieobject) 时,它只显示 32。我如何知道此类数据结构使用了多少内存。

最佳答案

我用

valgrind --tool=massif ./myprogram -opt arg1 arg2
ms_print massif.* | less -SR

为此。来自 this page 的示例输出

19.63^                                               ###                      
| #
| # ::
| # : :::
| :::::::::# : : ::
| : # : : : ::
| : # : : : : :::
| : # : : : : : ::
| ::::::::::: # : : : : : : :::
| : : # : : : : : : : ::
| ::::: : # : : : : : : : : ::
| @@@: : : # : : : : : : : : : @
| ::@ : : : # : : : : : : : : : @
| :::: @ : : : # : : : : : : : : : @
| ::: : @ : : : # : : : : : : : : : @
| ::: : : @ : : : # : : : : : : : : : @
| :::: : : : @ : : : # : : : : : : : : : @
| ::: : : : : @ : : : # : : : : : : : : : @
| :::: : : : : : @ : : : # : : : : : : : : : @
| ::: : : : : : : @ : : : # : : : : : : : : : @
0 +----------------------------------------------------------------------->KB 0 29.48

Number of snapshots: 25
Detailed snapshots: [9, 14 (peak), 24]

日志的其余部分详细说明了内存分配的最高百分位数,您可以具体查看哪种类型的类占用堆内存的百分比(以及根据调用堆栈分配的位置),例如:

--------------------------------------------------------------------------------
n time(B) total(B) useful-heap(B) extra-heap(B) stacks(B)
--------------------------------------------------------------------------------
10 10,080 10,080 10,000 80 0
11 12,088 12,088 12,000 88 0
12 16,096 16,096 16,000 96 0
13 20,104 20,104 20,000 104 0
14 20,104 20,104 20,000 104 0

99.48% (20,000B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->49.74% (10,000B) 0x804841A: main (example.c:20)
|
->39.79% (8,000B) 0x80483C2: g (example.c:5)
| ->19.90% (4,000B) 0x80483E2: f (example.c:11)
| | ->19.90% (4,000B) 0x8048431: main (example.c:23)
| |
| ->19.90% (4,000B) 0x8048436: main (example.c:25)
|
->09.95% (2,000B) 0x80483DA: f (example.c:10)
->09.95% (2,000B) 0x8048431: main (example.c:23)

关于c++ - 我的对象消耗了多少内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8094256/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com