Is this normal behavior for a std::vector?

前端 未结 3 1230
不思量自难忘°
不思量自难忘° 2021-01-16 03:40

I have a std::vector of a class called OGLSHAPE.

each shape has a vector of SHAPECONTOUR struct which has a vector of float and a vector of vector of double. it also

相关标签:
3条回答
  • 2021-01-16 03:54

    There are a few possible things at play here.

    First, the way memory works in most common C and C++ runtime libraries is that once it is allocated to the application from the operating system it is rarely ever given back to the OS. When you free it in your program, the new memory manager keeps it around in case you ask for more memory again. If you do, it gives it back for you for re-use.

    The other reason is that vectors themselves typically don't reduce their size, even if you clear() them. They keep the "capacity" that they had at their highest so that it is faster to re-fill them. But if the vector is ever destroyed, that memory will then go back to the runtime library to be allocated again.

    So, if you are not destroying your vectors, they may be keeping the memory internally for you. If you are using something in the operating system to view memory usage, it is probably not aware of how much "free" memory is waiting around in the runtime libraries to be used, rather than being given back to the operating system.

    The reason your memory usage increases slightly (instead of not at all) is probably because of fragmentation. This is a sort of complicated tangent, but suffice it to say that allocating a lot of small objects can make it harder for the runtime library to find a big chunk when it needs it. In that case, it can't reuse some of the memory it has laying around that you already freed, because it is in lots of small pieces. So it has to go to the OS and request a big piece.

    0 讨论(0)
  • 2021-01-16 03:58

    Calling std::vector<>::clear() does not necessarily free all allocated memory (it depends on the implementation of the std::vector<>). This is often done for the purpose of optimization to avoid unnessecary memory allocations.

    In order to really free the memory held by an instance just do:

    template <typename T>
    inline void really_free_all_memory(std::vector<T>& to_clear)
    {
        std::vector<T> v;
        v.swap(to_clear);
    }
    
    // ...
    std::vector<foo> objs;
    
    // ...
    // really free instance 'objs'
    really_free_all_memory(objs);
    

    which creates a new (empty) instance and swaps it with your vector instance you would like to clear.

    0 讨论(0)
  • 2021-01-16 04:08

    Use the correct tools to observe your memory usage, e.g. (on Windows) use Process Explorer and observe Private Bytes. Don't look at Virtual Address Space since that shows the highest memory address in use. Fragmentation is the cause of a big difference between both values.

    Also realize that there are a lot of layers in between your application and the operating system:

    • the std::vector does not necessarily free all memory immediately (see tip of hkaiser)
    • the C Run Time does not always return all memory to the operating system
    • the Operating System's Heap routines may not be able to free all memory because it can only free full pages (of 4 KB). If 1 byte of a 4KB page is stil used, the page cannot be freed.
    0 讨论(0)
提交回复
热议问题