search

Home  >  Q&A  >  body text

c++ 内存池是什么...

今天在知乎上问题问了一个问题, 轮子哥的回答里面提到的内存池的概念, 但是我其实不太清楚内存池到底是什么? 顺便求知乎提问中问题的解答...
链接如下 : 用c++写正则引擎构造nfa的时候到底该用智能指针吗?

天蓬老师天蓬老师2804 days ago611

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:29:19

    1) Apply for a large amount of memory from the operating system at one time, then build your object on top of it, and then return it to the operating system after use, to avoid frequent new delete overhead and memory fragmentation. These are considerations when your program needs performance very much.
    2) When using shared_ptr and unique_ptr, you need to understand who owns the resource. For example, if two Nodes are in the same status, their status is equal. At this time, you cannot know which Node should decide when to release the resources, so neither shared_ptr nor unique_ptr meets the application requirements. From an experience point of view, I suggest you implement it in the way of normal pointers, and finally traverse the entire graph to release resources, so that you can focus on implementing program logic.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:29:19

    The overhead of new and delete memory is relatively large, and it is relatively small to directly find a free memory block from the allocated memory array. So allocate a large memory in advance, divide it into small memory blocks, mark it as free when not in use, and retrieve it faster when in use. This is the prototype of the memory pool. I think simple and radical algorithms are indeed more effective, but pools with complex algorithms are not as good as the operating system's own new delete. After all, the operating system is constantly optimizing memory allocation and release algorithms, but it just chooses the most conservative strategy.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 14:29:19

    For your second question, the easiest way is to use vector as the main structure of the stack. If you need a more sophisticated implementation, you need to write a space configurator yourself (you can refer to the implementation of the double-layer space configurator in SGI STL)

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:29:19

    The one with the word pool is to create a collection, use a certain algorithm on this collection to optimize performance, memory pool, a lot of memory, manage and utilize it yourself. . Thread pool, many threads, management applications, no need to waste time in creation when using it. Timeliness is better

    reply
    0
  • Cancelreply