包含 unordered_map 的结构的大小(以字节为单位)
需要找到我实现的树数据结构占用的确切大小(以字节为单位)。节点结构如下
struct Node
{
int word;
int count;
unordered_map<int, Node*> map;
}node;
我一直在做的是 size(int)*2(for word and count) + map.bucket_count() * (sizeof(int) + sizeof(Node*)) 并对每个节点重复执行此操作。 如果我忽略了 unordered_map 中存储的元素开销,这是正确的方法吗?
另外,如果我是正确的,map.bucket_count() 会给出当前分配的存储桶数量,其中包括预分配的存储桶数量。我应该使用 map.size() 来代替它,这会忽略预先分配的存储桶吗?
或者代替这一切,使用像 MemTrack 这样的工具来查找所使用的内存是否更好?
Need to find the exact size in bytes, occupied by a tree data structure that I have implemented. Node Structure is as follows
struct Node
{
int word;
int count;
unordered_map<int, Node*> map;
}node;
What I have been doing is
size(int)*2(for word and count) + map.bucket_count() * (sizeof(int) + sizeof(Node*)) and repeatedly do this for each node.
Is this the correct way of doing this if i am neglecting the element overhead of storage in the unordered_map?
Also, if I am correct map.bucket_count() gives the number of buckets that are currently allocated that is including the pre- allocated ones. Should I be using map.size() instead which will ignore the pre-allocated buckets?
Or instead of all this, is it better to use tools like MemTrack to find the memory used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。从外部无法得知一个
unordered_map
或其他复杂的、不透明的对象占用了多少内存,并且一个好的内存分析器还可以向您显示内存分配器本身占用了多少开销。Yes. There's no telling from the outside how much memory an
unordered_map
or other complex, opaque object takes, and a good memory profiler might also show you how much overhead the memory allocator itself takes.