Boost logo

Boost Users :

Subject: Re: [Boost-users] Question regarding circular buffer usage
From: Travis Gockel (travis_at_[hidden])
Date: 2013-02-02 01:11:48


What you are seeing is *most likely* caused by your malloc or string
implementation (most likely not a bug, just an observation that differs
from what you would expect). If your strings are of variable size, memory
will most likely grow due to heap fragmentation. This should not grow
unbounded, since after the second pass through the loop, the chunk of
memory occupied by the first strings should be freed.

To show that circular_buffer works as documented, try a more simple object
like this:

int allocations = 0;

struct AllocCountingObj
{
    AllocCountingObj()
    {
        ++allocations;
    }

    AllocCountingObj(const AllocCountingObj&)
    {
        // still increment on copy-construct
        ++allocations;
    }

    ~AllocCountingObj()
    {
        --allocations;
    }
};

What compiler and STL implementation are you using and what are you using
to view the memory use?

On Fri, Feb 1, 2013 at 2:59 PM, Salivar.William <William.Salivar_at_[hidden]>wrote:

> I am using the circular buffer container to hold up to 10000 std string
> objects. I am finding that once the buffer has reached max capacity,
> memory continues to be consumed as I push new strings in. Is there
> something I’m missing? I stepped into the boost code and it does make a
> copy of the string using the std string operator so I would think it
> handles the string copy properly. The size of the strings are variable but
> I would expect the memory usage to be fairly stable when the buffer is full
> but that is not the case.****
>
> ** **
>
> Here’s basic usage****
>
> ** **
>
> Definition:****
>
> ** **
>
> boost::circular_buffer<std::string> m_message_buffer;****
>
> ** **
>
> Initialization:****
>
> ** **
>
> m_message_buffer.set_capacity (*10000*);****
>
> ** **
>
> Usage:****
>
> ** **
>
> *void* *myClass::writeToBuffer* (*const* *char* *message)****
>
> {****
>
> m_message_buffer.push_back (string (message));****
>
> }****
>
> ** **
>
> Boost code:****
>
> ** **
>
> *void* *push_back*(param_value_type item = value_type()) {****
>
> *if* (full()) {****
>
> *if* (empty())****
>
> *return*;****
>
> replace(m_last, item);****
>
> increment(m_last);****
>
> m_first = m_last;****
>
> } *else* {****
>
> m_alloc.construct(m_last, item);****
>
> increment(m_last);****
>
> ++m_size;****
>
> }****
>
> }****
>
> ** **
>
> ** **
>
> *void* *replace*(pointer pos, param_value_type item) {****
>
> *pos = item;****
>
> *#if* BOOST_CB_ENABLE_DEBUG****
>
> invalidate_iterators(iterator(*this*, pos));****
>
> *#endif*****
>
> }****
>
> ** **
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
Travis Gockel
λ Combinator




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net