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.
在看OSE的文檔時(OSE Architecture User's Guide),注意到一段關(guān)于內(nèi)存碎片的總結(jié),寫的很精辟,特摘抄如下:
原文如下(翻譯見原文后面的鏈接,某位老兄已然翻譯成中文,但不知道原文出自何處^_^)
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.
最終,內(nèi)存碎片會導致沒有內(nèi)存可申請,盡管總的剩余內(nèi)存看起來可以很充足(但感覺這個說法不嚴謹,物理內(nèi)存不夠時,系統(tǒng)會用虛擬內(nèi)存,硬盤頂替,所以系統(tǒng)應(yīng)該可以持續(xù)運行很長時間,但最終可能內(nèi)存耗盡)
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.內(nèi)存碎片是指系統(tǒng)中不可使用的空閑內(nèi)存。因為內(nèi)存分配器不能把這些內(nèi)存分配使用
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.
有朋友已經(jīng)翻譯出來,喜歡中文的請見:
http://blog./2010/04/25/%E8%BD%AC%EF%BC%9A%E5%86%85%E5%AD%98%E7%A2%8E%E7%89%87%E4%BA%A7%E7%94%9F%E5%8E%9F%E5%9B%A0%E5%8F%8A%E5%A4%84%E7%90%86%E6%8A%80%E6%9C%AF.html