跳转到内容

用户:LeafDuo/动态内存分配

维基百科,自由的百科全书

在计算机科学中,""在手动字词转换规则中检测到错误""(也称为""堆内存分配"")是一种在程序运行时分配存储器空间的技术。

In computer science, dynamic memory allocation (also known as heap-based memory allocation) is the allocation of memory storage for use in a computer program during the runtime of that program. It can be seen also as a way of distributing ownership of limited memory resources among many pieces of data and code.

Dynamically allocated memory exists until it is released either explicitly by the programmer, or by the garbage collector. This is in contrast to static memory allocation, which has a fixed duration. It is said that an object so allocated has a dynamic lifetime.

Details

[编辑]

The task of fulfilling an allocation request consists of finding a block of unused memory of sufficient size.

  • Problems during fulfilling allocation request
    • Internal and external fragmentation.
    • Allocator's metadata can inflate the size of (individually) small allocations;
      • Chunking attempts to reduce this effect.

Usually, memory is allocated from a large pool of unused memory area called the heap (also called the free store). Since the precise location of the allocation is not known in advance, the memory is accessed indirectly, usually via a pointer reference. The precise algorithm used to organize the memory area and allocate and deallocate chunks is hidden behind an abstract interface and may use any of the methods described below.

Efficiency

[编辑]

The dynamic memory allocation algorithm actually used can impact performance significantly and a study conducted in 1994 by Digital Equipment Corporation illustrates the overheads involved for a variety of allocators. The lowest average instruction path length required to allocate a single memory slot was 52 (as measured with an instruction level profiler on a variety of software)[1]

Implementations

[编辑]

Fixed-size-blocks allocation

[编辑]

Fixed-size-blocks allocation, also called memory pool allocation, uses a free list of fixed-size blocks of memory (often all of the same size). This works well for simple embedded systems.

Buddy blocks

[编辑]

In this system, memory is allocated from a large block in memory that is a power of two in size. If the block is more than twice as large as desired, it is broken in two. One of the halves is selected, and the process repeats (checking the size again and splitting if needed) until the block is just large enough.

All the blocks of a particular size are kept in a sorted linked list or tree. When a block is freed, it is compared to its buddy. If they are both free, they are combined and placed in the next-largest size buddy-block list. (When a block is allocated, the allocator will start with the smallest sufficiently large block avoiding needlessly breaking blocks)

See also

[编辑]
[编辑]

Further reading

[编辑]

References

[编辑]