摘录:
A memory allocator needs to adhere to some basic memory allocation rules. For example, all memory allocations
must start at an address that is divisible by 4, 8, or 16, depending on the processor architecture.
There may also be other reasons for the memory allocator to only assign blocks of certain predefined sizes
to its clients (cache-line size perhaps). When a client requests a block of 43 bytes, it may well get 44 or
48, or an even larger number. This extra space that results from rounding the requested size upwards is
called internal fragmentation.
External fragmentation is created when the memory allocator allocates blocks with unused gaps in between
them. This can occur, for example, when an application allocates three blocks in succession and then frees
the one in the middle. The memory allocator might reuse the middle block for future allocations, but it is
no longer possible to allocate a block as large as all free memory.
Definition of Memory Fragmentation
http://blog.csdn.net/dean_yanqing/article/details/6663775
在看OSE的文档时(OSE Architecture User's Guide),注意到一段关于内存碎片的总结,写的很精辟,特摘抄如下:
原文如下(翻译见原文后面的链接,某位老兄已然翻译成中文,但不知道原文出自何处^_^)
Why Is Memory Fragmentation Important
In embedded systems development, memory fragmentation knowledge is crucial
Ultimately, memory fragmentation leads to out-of-memory conditions, even when there might be plenty of free memory left in the system.
最终,内存碎片会导致没有内存可申请,尽管总的剩余内存看起来可以很充足(但感觉这个说法不严谨,物理内存不够时,系统会用虚拟内存,硬盘顶替,所以系统应该可以持续运行很长时间,但最终可能内存耗尽)
What Is Memory Fragmentation
Fragmented memory is the term used to describe all of a system's unusable free memory. These resources
remain unused, because the memory allocator that you are using is — for one reason or another — unable
to make this memory available to you.内存碎片是指系统中不可使用的空闲内存。因为内存分配器不能把这些内存分配使用
Compile Time vs. Run Time
Memory is allocated in many different contexts. The programmer can (by way of the compiler and linker)
allocate memory for data in structures, unions, arrays, and scalars as local, static, or global variables. The
programmer can also allocate memory dynamically in run time using calls such as malloc(). When the
compiler and linker perform the memory allocation function, memory fragmentation does not arise, because
the compiler understands the lifetime of the data. The data lifetime offers the nice advantage of being
stackable (last in, first out). This makes it possible for the memory allocator to work in a very efficient and
non-fragmenting manner. Generally, memory allocations issued during run time are not stackable. Memory
allocations are independent in time, which makes the problem extremely difficult to resolve.
Internal/ External Fragmentation and Overhead
Memory allocators waste memory in three basic ways:
• Overhead
• Internal fragmentation
• External fragmentation
The memory allocator needs to store some data describing the state of its allocations. That space is generally
called overhead. This is information about the location, size, and ownership of any free blocks and about
other internal states. A run-time allocator typically has no better place to store this overhead information
than in the memory it manages.
A memory allocator needs to adhere to some basic memory allocation rules. For example, all memory allocations
must start at an address that is divisible by 4, 8, or 16, depending on the processor architecture.
There may also be other reasons for the memory allocator to only assign blocks of certain predefined sizes
to its clients (cache-line size perhaps). When a client requests a block of 43 bytes, it may well get 44 or
48, or an even larger number. This extra space that results from rounding the requested size upwards is
called internal fragmentation.
External fragmentation is created when the memory allocator allocates blocks with unused gaps in between
them. This can occur, for example, when an application allocates three blocks in succession and then frees
the one in the middle. The memory allocator might reuse the middle block for future allocations, but it is
no longer possible to allocate a block as large as all free memory.
External fragmentation is created when the memory allocator allocates blocks with unused gaps in between
them. This can occur, for example, when an application allocates three blocks in succession and then frees
the one in the middle. The memory allocator might reuse the middle block for future allocations, but it is
no longer possible to allocate a block as large as all free memory.
Provided the memory allocator doesn't change its implementation or rounding policy in run time, the
overhead and internal fragmentation remains constant throughout an application's lifetime. While overhead
and internal fragmentation may be undesired (because they waste memory), external fragmentation is the
real enemy of embedded systems. External fragmentation is the allocation problem that kills systems.
Whenever the word fragmentation is used throughout the rest of this discussion, think external fragmentation
Definition
Now we are ready for a definition of (external) memory fragmentation. Other definitions are possible, but
this is the one most commonly used. It applies to external fragmentation, but can be modified to include
internal fragmentation (by including internal fragmentation in the denominator).
Figure 5.1 Definition of Memory Fragmentation
Fragmentation is a fraction between 0 and 1. A system in which fragmentation = 1 (100%) is completely
out of memory. With all free memory in a single block (the largest), fragmentation is 0%. With one quarter
of all free memory in the largest block, fragmentation is 75%. For example, fragmentation is 99% in a
system with 5 MB of free memory, when the largest block available for allocation is 50 KB.
有朋友已经翻译出来,喜欢中文的请见: