記憶體片段(Memory Fragmentation)(1)

來源:互聯網
上載者:User

  在看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.記憶體片段是指系統中不可使用的空閑記憶體。因為記憶體 Clerk不能把這些記憶體配置使用

  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.

有朋友已經翻譯出來,喜歡中文的請見:

http://blog.201314.info/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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.