请输入您要查询的百科知识:

 

词条 memory pool
释义

Memory pools中文意思为内存池,也叫动态内存分配,内存池(Memory Pool)是一种内存分配方式。 通常我们习惯直接使用new、malloc等API申请分配内存,这样做的缺点在于:由于所申请内存块的大小不定,当频繁使用时会造成大量的内存碎片并进而降低性能。 内存池则是在真正使用内存之前,先申请分配一定数量的、大小相等(一般情况下)的内存块留作备用。当有新的内存需求时,就从内存池中分出一部分内存块,若内存块不够再继续申请新的内存。这样做的一个显著优点是尽量避免了内存碎片,使得内存分配效率得到提升。

Memory pool

also called fixed-size-blocks allocation, allow dynamic memory allocationcomparable to mallocor C++'s operator new. As those implementations suffer from fragmentationbecause of variable block sizes, it can be impossible to use them in a real time systemdue to performance. A more efficient solution is preallocating a number of memory blocks with the same size called the memory pool. The application can allocate, access, and free blocks represented by handlesat runtime.

Many real-time operating systemsuse memory pools, such as the Transaction Processing Facility.

Contents[hide]1Sample memory pool implementationA simple memory pool module can allocate for example 3 pools at compile timewith block sizes optimized for the application, which deploys the module. The application can allocate, access and free memory with the following interface:

To allocate memory from the pools. The function will determine the pool, where the required block fits in. If all blocks of that pool are already reserved, the function tries to find one in the next bigger pool(s). An allocated memory block is represented with a handle.To get an access pointer to the allocated memory .To free the formerly allocated memory block.The handle can for example be implemented with an unsigned int. The module can interpret the handle internally by dividing it into pool index, memory block index and a version. The pool and memory block index allow fast access to the corresponding block with the handle, while the version, which is incremented at each new allocation, allows detection of handles whose memory block is already freed (caused by handles retained too long).

[edit]Memory pool vs malloc

Benefits

Memory pools allow memory allocation with constant execution time (nofragmentation). The memory release for thousands of objects in a pool is just one operation, not one by one if mallocis used to allocate memory for each object.Memory pools can be grouped in hierarchical tree structures, which is suitable for special programming structures like loopsand recursions.Fixed-size block memory pools do not need to store allocation metadata for each allocation, describing characteristics like the size of the allocated block. Particularly for small allocations, this provides a substantial space savings.Drawbacks

Memory pools may need to be tuned for the application which deploys them.

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/12/23 6:07:32