开发者

Memory SPIKE - Boost ASIO ASYNC read

开发者 https://www.devze.com 2023-04-08 13:45 出处:网络
Wrote a Server which just reads data from a client: Using a boost::array buffer Started the server and system monitor shows 1MB of usage.

Wrote a Server which just reads data from a client:

Using a boost::array buffer

Started the server and system monitor shows 1MB of usage.

1.) Just do an async_read_some and do a handleRead in which I again call the asyncRead function.

void asyncRead() {
    m_socket->async_read_some(
        boost::asio::buffer(m_readBuffer, READ_BLOCK_SIZE),
        m_strand->wrap(boost::bind(&ConnectionHandler::handleRead,
                                   shared_from_this(),
                                   boost::asio::placeholders::error,
                                   boost::asio::placeholders::bytes_transferred))
    );
}

and in handleRead I verify if there are any errors or not and if there aren't any I simply issue another asyncRead().

2.) Kept sending frames ( data of size around 102 bytes ).

At end of test for 10000 Frames. Total Sent size = 102*10000 Total Read Size = 102*10000

But, the memory usage in system monitor spikes up to 7.8 Mb .

Couldn't figure out the cause of this increase. The different aspects tried out are: 1.) Number of connections being ma开发者_JAVA技巧de - only 1. 2.) Verified closing of connection - yes. 3.) stopped even the ioServic but still no change.

On a 2nd run of the client, I see the memory increasing. What could be the case? I am using a boost::array which is a stack variable and simply just reading. No other place there is a buffer being initialized.


Raja,

First of all, are you aware that async_read_some does not guarantee that you will read the entire READ_BLOCK_SIZE? If you need that guarantee, I would suggest you to use async_read instead.

Now, back to the original question, your situation is quite typical. So, basically, you need a container (array) that will hold the data until is sent, and then you need to get rid of it.

I strongly suggest you switching to boost shared_array. You can use it in the same way as boost array, but it has a built-in reference counter, so the object will be deleted when it is not needed anymore. This should solve your memory leak.

0

精彩评论

暂无评论...
验证码 换一张
取 消