9

I'm having an issue with (specifically the MSFT VS 10.0 implementation of) std::unique_ptrs. When I create a std::list of them, I use twice as much memory as when I create a std::list of just the underlying object (note: this is a big object -- ~200 bytes, so it's not just an extra reference counter lying around).

In other words, if I run:

std::list<MyObj> X;
X.resize( 1000, MyObj());

my application will require half as much memory as when I run:

std::list<std::unique_ptr<MyObj>> X;
for ( int i=0; i<1000; i++ ) X.push_back(std::unique_ptr<MyObj>(new MyObj()));

I've checked out the MSFT implementation and I don't see anything obvious -- any one encountered this and have any ideas?

EDIT: Ok, to be a bit more clear/specific. This is clearly a Windows memory usage issue and I am obviously missing something. I have now tried the following:

  1. Create a std::list of 100000 MyObj
  2. Create a std::list of 100000 MyObj*
  3. Create a std::list of 100000 int*
  4. Create a std::list of 50000 int*

In each case, each add'l member of the list, whether a pointer or otherwise, is bloating my application by 4400(!) bytes. This is in a release, 64-bit build, without any debugging information included (Linker > Debugging > Generate Debug Info set to No).

I obviously need to research this a bit more to narrow it down to a smaller test case.

For those interested, I am determining application size using Process Explorer.

Turns out it was entirely heap fragmentation. How ridiculous. 4400 bytes per 8 byte object! I switched to pre-allocating and the problem went away entirely -- I am used to some inefficiency in relying on per-object allocation, but this was just ridiculous.

MyObj implementation below:

class   MyObj
{
public:
    MyObj() { memset(this,0,sizeof(MyObj)); }

    double              m_1;
    double              m_2;
    double              m_3;
    double              m_4;
    double              m_5;
    double              m_6;
    double              m_7;
    double              m_8;
    double              m_9;
    double              m_10;
    double              m_11;           
    double              m_12;           
    double              m_13;
    double              m_14;
    double              m_15;
    double              m_16;
    double              m_17;
    double              m_18;
    double              m_19;
    double              m_20;
    double              m_21;
    double              m_22;
    double              m_23;
    CUnit*              m_UnitPtr;
    CUnitPos*           m_UnitPosPtr;
};
11
  • 2
    Do you see the same memory usage using a raw MyObj*?
    – bdonlan
    Nov 28, 2011 at 16:33
  • You mean if I add 1000 MyObjs to a std::list using new? I see the same usage as the normal list case, not the std::unique_ptr case.
    – nadime
    Nov 28, 2011 at 16:33
  • Try MSVC11, they optimized quite a few data structures. Also, unique_ptr doesn't have a reference counter. Can you post the definition of MyObj too, please?
    – Kerrek SB
    Nov 28, 2011 at 16:34
  • 1
    Do you think it would be feasible to demonstrate this behaviour with a short self-contained program, so that others could experiment with it?
    – NPE
    Nov 28, 2011 at 16:36
  • 2
    Also, I'd like to know how you determined this fact.
    – Puppy
    Nov 28, 2011 at 16:38

3 Answers 3

3

The added memory is likely from heap inefficiencies - you have to pay extra for each block you allocate due to internal fragmentation and malloc data. You're performing twice the amount of allocations which is going to incur a penalty hit.

For instance, this:

for(int i = 0; i < 100; ++i) {
  new int;
}

will use more memory than this:

new int[100];

Even though the amount allocated is the same.


Edit:

I'm getting around 13% more memory used using unique_ptr using GCC on Linux.

4
  • I agree that there isn't necessarily a great reason to use std::unique_ptr here (although I'm not sure how you determine that without more information from me), but I've gotten in the habit of using them as my case object collection and the magnitude of the heap inefficiency here is shocking. Also, there's not a great reason why the std::unique_ptr would be so much less efficient than a straight object allocation.
    – nadime
    Nov 28, 2011 at 16:47
  • unique_ptr is probably used in this case to enforce ownership. If someone takes the objects out of this list then they will have to maintain it in some other manner or it will go out of scope and automatically free the memory. Personally I would use boost::ptr_list myself for the same reason, but it has nicer syntax IMO.
    – Dennis
    Nov 28, 2011 at 16:52
  • much harder to use ptr_list when you embed pointers to the underlying objects into other objects. ownership can be maintained in the main structure (here a std::list of unique_ptrs), while allowing other objects to modify in every way but creation/deletion.
    – nadime
    Nov 28, 2011 at 16:59
  • @nadime Possible for you to use a memory pool?
    – Pubby
    Nov 28, 2011 at 17:09
2

std::list<MyObj> contains N copies of your object (+ the information needed for the pointers of the list).

std::unique_ptr<MyObj> contains a pointer to a instance of your object. (It should only contain a MyObj*).

So a std::list<std::unique_ptr<MyObj>> is not directly equivalent to your first list. std::list<MyObj*> should give the same size as the std::unque_ptr list.

After verifying the implementation, the only thing that could be embedded next to the pointer of the object itself, could be the 'deleter', which in the default case is a empty object that calls operator delete.

Do you have a Debug or a Release build?

7
  • I'm not worried about the size of the list itself. I'm worried about the memory usage of my application. The previous answerer points out that differences may be due to heap inefficiencies, though the magnitude of the issue here is shocking.
    – nadime
    Nov 28, 2011 at 16:46
  • Is it a debug or relase build? In release there should at best be a deleter involved. And even that one is compiled out through templates. Nov 28, 2011 at 16:47
  • release, that's what's so shocking.
    – nadime
    Nov 28, 2011 at 16:48
  • And std::list<MyObj*> is the same size as std::list<MyObj> ? Nov 28, 2011 at 16:49
  • 1
    The idea is, that when its a heap inefficiency, it should also show up when you are using plain old pointers. Something like std::list<Foo> l2; for (unsigned int i = 0; i != 100; ++i) l2.push_back(new Foo); Your question is about unique_ptr being inefficient. But maybe your comment about heap allocations is right, then this should show it. Nov 28, 2011 at 16:56
2

This isn't an answer, but it doesn't fit in a comment and it might be illustrative.

I cannot reproduce the claim (GCC 4.6.2). Take this code:

#include <memory>
#include <list>

struct Foo { char p[200]; };

int main()
{
  //std::list<Foo> l1(100);

  std::list<std::unique_ptr<Foo>> l2;
  for (unsigned int i = 0; i != 100; ++i) l2.emplace_back(new Foo);
}

Enabling only l1 produces (in Valgrind):

total heap usage: 100 allocs, 100 frees, 20,800 bytes allocated

Enabling only l2 and the loop gives:

total heap usage: 200 allocs, 200 frees, 21,200 bytes allocated

The smart pointers take up exactly 4 × 100 bytes.

In both cases, /usr/bin/time -v gives:

Maximum resident set size (kbytes): 3136

Further more, pmap shows in both cases: total 2996K. To confirm, I changed the object size to 20000 and the number of elements to 10000. Now the numbers are 198404K vs 198484K: Exactly 80000B difference, 8B per unique pointer (presumably there's some 8B alignment going on in the allocator of the list). Under the same changes, the "maximum resident set size"s reported by time -v are now 162768 vs 164304.

7
  • 2
    That's showing amount allocated, not the amount of memory being used.
    – Pubby
    Nov 28, 2011 at 16:43
  • @Pubby: Hm, interesting. How would you measure total memory usage?
    – Kerrek SB
    Nov 28, 2011 at 16:45
  • I don't know of the best way, but command prompt gives an estimate.
    – Pubby
    Nov 28, 2011 at 16:47
  • Sure, but I'm not sure there's a need. This isn't necessarily a big problem for me, the reason I started investigating is that I get vastly different usage on a Windows 7 box vs. a Windows 2008 server box. Very weird.
    – nadime
    Nov 28, 2011 at 17:33
  • @nadime: We'd need a more accurate description of how you determine the memory usage and what the exact numbers are... Maybe Process Explorer has something useful to say?
    – Kerrek SB
    Nov 28, 2011 at 17:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.