0

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.

Program with empty main body takes 1KB of memory.

The 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).

After reserving 200,000 elements the vector v takes 6400KB of memory. Thus, total memory until now is 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. If I replace string with int so the total usage memory will be as it supposed to be 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.   }
5
  • How are you verifying the memory usage? Commented Jan 14, 2014 at 6:08
  • With Task Manager. I know that maybe this is not the most accurate tool, but it show very deterministic memory behavior on various machines (with same architecture of course). Commented Jan 14, 2014 at 6:12
  • Are you running with full optimizations so no unnecessary copies will be made? Can you try replacing the push_back() calls with a single resize(100000)? Commented Jan 14, 2014 at 6:16
  • 1
    In task manager which "memory usage" are you looking at? Also, are you compiling in debug mode or release mode? There could be debug build only members etc. Commented Jan 14, 2014 at 6:38
  • @Brandon, you are right! I didn't notice that I was running in Debug:( Sorry. Commented Jan 15, 2014 at 7:35

2 Answers 2

2

A std::string contains an additional allocated memory component to contain the contents of the 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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's why sizeof(Bag)=32. (= 16 bytes of internal buffer in string + other 16 for pointer and other stuff). So, when I reserve 200,000 of 32 bytes and then adds 100,000 Bags no allocation in string should occur because empty string will stored in internal buffer. So what is allocated when adding Bag is added to vector?
@theateist I can't say without looking at the particular implementation of std::string that you're using. You haven't even mentioned which compiler you have.
I updated my post. The compiler is Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
0

reserve() function allocates memory for the bundle of Bag objects. This chunk of memory contains space for all data members of std::string. std::string allocates memory for keeping string data and has pointer to it as data member. The memory chunks for std::string data is addition space you observed. In case of int data members: it has no additional buffers so there is no extra memory allocated.

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

3 Comments

see my comment to @Mark. The string has 16 bytes of internal memory in stack. No allocation is made when adding Bag to vector
I know that it will grow because the new string is 26 bytes length which is greater then the length of the internal buffer of the string(in my case its 16) and that's why it allocated space in heap. But, I insert empty string into vector whose length is smaller than 16, so it shouldn't allocate nothing!
@theateist Please check capacity() function meaning. It's not related to the space where string object resided itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.