简体   繁体   English

为什么将空结构插入保留向量会增加内存?

[英]Why inserting empty struct to reserved vector increases memory?

EDITED The code was compiled with Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86 in VS2010 as 32bit and runs on Win7 64bit machine. EDITED代码是使用Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86 ,VS2010中的Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86为32位,并在Win7 64位机器上运行。

Program with empty main body takes 1KB of memory. 带空main程序需要1KB的内存。

The sizeof(Bag)=32 . sizeof(Bag)=32 (=16 bytes of internal buffer for short strings + 4 byte of pointer to memory if string bigger than 16 bytes is added + other internal stuff of string). (=短字符串的16字节内部缓冲区+如果添加大于16字节的字符串+字符串的其他内部内容,则指向内存的指针的4字节)。

After reserving 200,000 elements the vector v takes 6400KB of memory. 在保留200,000个元素后,向量v占用6400KB的内存。 Thus, total memory until now is 7400KB. 因此,到目前为止总内存为7400KB。

What I don't understand is why after inserting 100,000 elements (which is smaller than reserved capacity of v ) into v the memory usage increases to 14,800KB. 我不明白的是为什么将10万件(比预留容量小后v )为v内存使用率增加至14,800KB。 If I replace string with int so the total usage memory will be as it supposed to be 1,800KB (= 1000KB + 200*4B) 如果我用int替换string ,那么总的使用内存将是1,800KB(= 1000KB + 200 * 4B)

1.    struct Bag
2.    {
3.       string s;      
4.    };

5.    vector<Bag> v;
6.    v.reserve(200000);

7.    for(int i = 0; i < 100000; ++i)
8.    {
9.      v.push_back(Bag());
10.   }

A std::string contains an additional allocated memory component to contain the contents of the string; std::string包含一个额外的已分配内存组件,用于包含字符串的内容; the actual string object itself is only half the story. 实际的字符串对象本身只是故事的一半。 Some implementations have an optimization to eliminate the extra overhead for very short strings, but this is not guaranteed. 某些实现具有优化以消除非常短的字符串的额外开销,但这不能保证。

The amount of memory used by each allocation will depend on the minimum allocation characteristics for your platform. 每个分配使用的内存量取决于平台的最小分配特征。

reserve() function allocates memory for the bundle of Bag objects. reserve()函数为Bag对象包分配内存。 This chunk of memory contains space for all data members of std::string . 这块内存包含std::string所有数据成员的空间。 std::string allocates memory for keeping string data and has pointer to it as data member. std::string为保持字符串数据分配内存,并指向它作为数据成员。 The memory chunks for std::string data is addition space you observed. std::string数据的内存块是您观察到的额外空间。 In case of int data members: it has no additional buffers so there is no extra memory allocated. 对于int数据成员:它没有额外的缓冲区,因此没有分配额外的内存。

The following snippet shows that string has extra bytes allocated: 以下代码段显示该字符串已分配额外字节:

#include <string>
#include <iostream>

int main() {
        std::string s;
        std::cout << "[1] Buf: " << s.capacity() << std::endl;
        s = "Now it's contain some data";
        std::cout << "[2] Buf: " << s.capacity() << std::endl;
        return 0;
}

The output is as follows: 输出如下:

./a.out
[1] Buf: 22
[2] Buf: 47

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM