内存碎片@ boost::asio?
我几乎被一个从未得到答案的问题所困扰,这个问题解决了一个极其重要的问题; boost::asio 处的内存碎片。
在文档和 SO 中都找不到任何内容。
boost::asio 的异步函数,例如 async_write() 和 async_write() 。 async_read_some() 总是分配一些东西。 (在我的例子中,在 VC9 调试版本中分别为 144 和 96 字节)。
我怎么知道?
我将客户端连接到该库提供的“回显服务器”示例。
我在“operator new(size_t size)”代码处的“new.cpp”处放置了一个断点。
然后我发送“123”。断点已命中!
现在使用堆栈跟踪我可以清楚地看到“新”调用的根源来自 async_write() &我在函数处理程序中进行的 async_read_some() 调用。
所以内存碎片迟早会出现,所以我不能使用ASIO,但愿我可以!
我想指出的是,我知道异步操作至少在 IOCP 上需要在操作期间使用分配的结构,直到完成(调用处理程序时)。
例如,对于 IOCP,每个操作(读/写)都必须有一个 OVERLAPPED 结构。
我怀疑这就是我有上述小分配的原因,谁担心我担心内存碎片。
那么我如何汇集这些结构呢?当然是平台无关的。
有什么想法吗?有什么有用的代码示例吗?我确信你们中的一些人已经解决了这个问题。
I'm pretty much stuck with a question I never got an answer for, a question which addresses an extremely important issue; memory fragmentation at boost::asio.
Found nothing at the documentation nor here at SO.
The async functions at boost::asio, for example async_write() & async_read_some() always allocate something. (in my case it's 144 & 96 bytes respectively, in VC9 Debug build).
How do I know about it?
I connect a client to the "echo server" example provided with this library.
I put a breakpoint at "new.cpp" at the code of "operator new(size_t size)".
Then I send "123". Breakpoint is hit!
Now using the stack trace I can clearly see that the root to the "new" call is coming from the async_write() & async_read_some() calls I make in the function handlers.
So memory fragmentation will come sooner or later, thus I can't use ASIO, and I wish I could!
I want to note that I understand that an async operation requires, at least at IOCP, an allocated structure to be used during the operation, until completion (when handler is called).
For example with IOCP you must have an OVERLAPPED structure for each operation (read/write).
I suspect this is the reason that I have the above small allocations, who worries me about memory fragmentation.
How do I pool these strctures then? Platform-independent of course.
Any idea? Any helpful code example? I'm sure some of you already solved this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要使用 asio 自定义内存分配功能,您是否阅读过 文档?还有一个相当简单的 示例。
Sounds like you need to use the asio custom memory allocation capabilities, have you read the documentation? There's also a fairly straightforward example.