04-05-2012, 03:09 PM
Memory Allocation
6_AllocationChapter.pdf (Size: 246.07 KB / Downloads: 82)
Consequences
Choosing an appropriate allocation strategy can ensure that the program meets its memory
requirements, and that its runtime demands for memory are predictable. Fixed allocation
strategies can increase a programs real-time responsiveness and time performance, while
variable strategies can ensure the program can scale up to take advantage of more memory if it
becomes available, and avoid allocating memory that is unused.
However: Supporting more than one allocation strategy requires programmer effort to implement.
The system developers must consider the allocation strategies carefully, which takes
significantly more work than just using the default allocation technique supported by the
programming language. This approach also requires programmer discipline since developers
must ensure that they do use suitable allocation strategies. Allocating large amounts of memory
as a system beings executing can increase its start-up time, while relying on dynamic allocation
can make memory use hard to predict in advance.
Implementation
As with all patterns, the patterns in this chapter can be applied together, often with one pattern
relying on another as part of its implementation. The patterns in this chapter can be applied in a
particularly wide variety of permutations. For example, you could have very large object
allocated on the heap (VARIABLE ALLOCATION), which contains an embedded array of subobjects
(FIXED ALLOCATION) that are allocated internally by the large containing object
(POOLED ALLOCATION). Here, the FIXED ALLOCATION and POOLED ALLOCATION patterns are
implemented within the large object, and each pattern is supported by other patterns in their
implementation.
You can use different patterns for different instances of the same class. For example, different
instances of the Integer class could be allocated on the heap (VARIABLE ALLOCATION), on the
stack (MEMORY DISCARD), or embedded in another object (FIXED ALLOCATION), depending on
the requirements of each particular use.
You can also choose between different patterns depending on circumstance. For example, a
Smalltalk networking application was required to support a guaranteed minimum throughput,
but could improve its performance if it could allocate extra buffer memory. The final design
pre-allocated a pool of five buffers (FIXED ALLOCATION); if a new work item arrived while all
the buffers were in use, and more memory was available, the system dynamically allocated
further buffers (VARIABLE ALLOCATION).
Here are some further issues to consider when designing memory
1. Fragmentation
Fragmentation is a significant problem with dynamic memory allocation. There are two kinds
of fragmentation: internal fragmentation, when a data structure does not use all the memory it
has been allocated; and external fragmentation, when memory lying between two allocated
structures cannot be used, generally because it is too small to store anything else. For example,
if you delete an object that occupies the space between two other objects, some of the deleted
object’s space will be wasted, unless