Dynamic memory allocation

From Free net encyclopedia

(Redirected from Memory allocation)

In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It is a way of distributing ownership of limited memory resources among many pieces of data and code. A dynamically allocated object remains allocated until it is deallocated explicitly, either by the programmer or by a garbage collector; this is notably different from automatic and static memory allocation. It is said that such an object has dynamic lifetime.

The task of fulfilling an allocation request, which involves finding a block of unused memory of a certain size in the heap, is a difficult problem. A wide variety of solutions have been proposed, including:

The main problem for most dynamic memory allocation algorithms is to avoid both internal and external fragmentation while keeping both allocation and deallocation efficient. Also, most algorithms in use have the problem that a large number of small allocations can cause wasted space due to collecting metadata; thus most programmers avoid this, sometimes by using a strategy called chunking.

Contents

Fixed-size-blocks allocation

One solution is to have a LIFO linked list of fixed size blocks of memory. This works astonishingly well for simple embedded systems.

Buddy blocks

Another solution is to have a binary buddy block allocator. In this system, memory is allocated from a large block of 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 buddies 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)

Note that buddy block allocators are not unique to real-time operating systems, they are also used in conventional operating systems (such as the Linux kernel).

Heap-based memory allocation

In heap-based memory allocation, memory is allocated from a large pool of unused memory area called the heap (this has nothing to do with the heap data structure). The size of the memory allocation can be determined at run-time, and the lifetime of the allocation is not dependent on the current procedure or stack frame. The region of allocated memory is accessed indirectly, usually via a 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 above.

In constrast, the call stack memory is usually of limited size and the lifetime of the allocation depends on the duration of the corresponding functions.

See also

References

pl:Alokacja pamięci