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.
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).
In contrast, the call stack memory is usually of limited size and the lifetime of the allocation depends on the duration of the corresponding functions.
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Dynamic memory allocation".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world