Dynamic memory allocation
![]() |
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 run-time 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 exist during fulfilling allocation request
- Internal and external fragmentation.
- Reduction needs special care, thus making implementation more complex (see algorithm efficiency).
- Allocator's metadata can inflate the size of (individually) small allocations;
- Chunking attempts to reduce this effect.
- Internal and external fragmentation.
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).
Dynamic memory allocation in C
Many languages permit a programmer to specify an array size at run time. Such languages have the ability to calculate and assign during executions, the memory space required by the variables in the program. But c inherently does not have this facility but supports with memory management functions, which can be used to allocate and free memory during the program execution. The following functions are used in c for purpose of memory management[2]
Function | Task of the Function |
---|---|
malloc | Allocates memory to the variables and returns a pointer to the first byte in allocated space |
calloc | Allocates memory to the variable & initialized it to zero |
free | Free initially allocated space |
realloc | Modify the size of previously allocated space |
See also
- Heap (data structure)
- Automatic memory allocation
- Dynamic array
- Garbage collection
- Hazard pointer
- Heap overflow
- Hoard memory allocator
- Java Virtual Machine heap
malloc
- Memory pool
mmap
- new (C++)
- obstack
- Slab allocation
- SLOB
- Stack-based memory allocation
References
![]() | This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (September 2009) |
- Donald Knuth. Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.5: Dynamic Storage Allocation, pp. 435–456.
- Simple Memory Allocation Algorithms[dead link] on OSDEV Community
- Wilson, P.R. (1995). "Dynamic Storage Allocation: A Survey and Critical Review". Memory Management: International Workshop, Iwmm'95, Kinross, Uk, September 27-29, 1995: Proceedings. Springer. ISBN 9783540603689. Retrieved 2008-01-06.
{{cite journal}}
: Unknown parameter|coauthors=
ignored (|author=
suggested) (help) - Berger, E.D. (2001). "Composing high-performance memory allocators". ACM SIGPLAN Notices. 36 (5): 114–124. doi:10.1145/381694.
{{cite journal}}
: Unknown parameter|coauthors=
ignored (|author=
suggested) (help) - Berger, E.D. (2002). "Reconsidering custom memory allocation". Proceedings of the 17th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications. ACM Press New York, NY, USA. pp. 1–12.
{{cite conference}}
: Cite has empty unknown parameter:|conferenceurl=
(help); Unknown parameter|booktitle=
ignored (|book-title=
suggested) (help); Unknown parameter|coauthors=
ignored (|author=
suggested) (help)
Further reading
- "Dynamic Storage Allocation: A Survey and Critical Review", Department of Computer Sciences University of Texas at Austin