如何计算碎片?

发布于 2024-10-10 07:29:20 字数 337 浏览 3 评论 0原文

想象一下,您有一些内存包含一堆字节:

++++ ++-- ---+ +++-
-++- ++++ ++++ ----
---- ++++ +

让我们说 + 表示已分配,- 表示空闲。

我正在寻找如何计算碎片百分比公式

背景

我正在为具有静态内存的嵌入式设备实现小型动态内存管理。我的目标是拥有可以用来存储少量数据的东西。大多数是通过无线连接传入的数据包,每个数据包大约 128 字节。

Imagine you have some memory containing a bunch of bytes:

++++ ++-- ---+ +++-
-++- ++++ ++++ ----
---- ++++ +

Let us say + means allocated and - means free.

I'm searching for the formula of how to calculate the percentage of fragmentation.

Background

I'm implementing a tiny dynamic memory management for an embedded device with static memory. My goal is to have something I can use for storing small amounts of data. Mostly incoming packets over a wireless connection, at about 128 Bytes each.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

も星光 2024-10-17 07:29:20

正如 R. 所说,这完全取决于您所说的“碎片百分比”的含义 - 但您可以使用一个简单的公式:

(free - freemax)
----------------   x 100%    (or 100% for free=0)
    free

其中

free     = total number of bytes free
freemax  = size of largest free block

这样,如果所有内存都在一个大块中,则碎片为 0%,并且如果内存全部被分割成数百个小块,就会接近100%。

As R. says, it depends exactly what you mean by "percentage of fragmentation" - but one simple formula you could use would be:

(free - freemax)
----------------   x 100%    (or 100% for free=0)
    free

where

free     = total number of bytes free
freemax  = size of largest free block

That way, if all memory is in one big block, the fragmentation is 0%, and if memory is all carved up into hundreds of tiny blocks, it will be close to 100%.

十秒萌定你 2024-10-17 07:29:20

计算当前内存布局可以容纳多少个 128 字节数据包。
设该数字为n。

计算在内存布局中可以容纳多少个 128 字节数据包,该内存布局具有与当前分配的字节数相同的字节数,但没有空洞(例如,将所有 + 移至左侧)。
设该数字为 N。

您的“碎片率”将为 alpha = n/N

Calculate how many 128 bytes packets you could fit in the current memory layout.
Let be that number n.

Calculate how many 128 bytes packets you could fit in a memory layout with the same number of bytes allocated than the current one, but with no holes (that is, move all the + to the left for example).
Let be that number N.

Your "fragmentation ratio" would be alpha = n/N

昇り龍 2024-10-17 07:29:20

如果您的分配大小大致相同,只需将内存分成 TOTAL/MAXSIZE 块,每个块由 MAXSIZE 字节组成。那么碎片化就无关紧要了。

一般来说,回答你的问题,“碎片”没有神奇的数字。您必须评估不同函数在反映内存碎片程度方面的优点。这是我推荐的一个,作为大小 n 的函数:

fragmentation(n) = -log(n * number_of_free_slots_of_size_n / total_bytes_free)

请注意,log 只是将事物映射到“0 到无穷大”的范围;您不应该在实践中实际评估这一点。相反,您可以简单地评估:

freespace_quality(n) = n * number_of_free_slots_of_size_n / total_bytes_free

1.0 是理想的(能够分配最大可能数量的大小为 n 的对象),0.0 非常糟糕(无法分配任何)。

If your allocations are all roughly the same size, just split your memory up into TOTAL/MAXSIZE pieces each consisting of MAXSIZE bytes. Then fragmentation is irrelevant.

To answer your question in general, there is no magic number for "fragmentation". You have to evaluate the merits of different functions in reflecting how fragmented memory is. Here is one I would recommend, as a function of a size n:

fragmentation(n) = -log(n * number_of_free_slots_of_size_n / total_bytes_free)

Note that the log is just there to map things to a "0 to infinity" scale; you should not actually evaluate that in practice. Instead you might simply evaluate:

freespace_quality(n) = n * number_of_free_slots_of_size_n / total_bytes_free

with 1.0 being ideal (able to allocate the maximum possible number of objects of size n) and 0.0 being very bad (unable to allocate any).

只为一人 2024-10-17 07:29:20

如果您有 [++++++-----++++--++-++++++++--------+++++] 并且您想要测量可用空间(或任何其他分配)的碎片
您可以测量平均连续块大小
总块数/连续块数。

在这种情况下,它将是
4/(5 + 2 + 1 + 8) / 4 = 4

If you had [++++++-----++++--++-++++++++--------+++++] and you wanted to measure the fragmentation of the free space (or any other allocation)
You could measure the average contiguous block size
Total blocks / Count of contiguous blocks.

In this case it would be
4/(5 + 2 + 1 + 8) / 4 = 4

很糊涂小朋友 2024-10-17 07:29:20

基于 R.. GitHub STOP HELPING ICE 的回答,我想出了以下将碎片计算为单个百分比数字的方法:

碎片= 1 - 1/n * FreeSlots(i)/IdealFreeSlots(i) 的 i=1..n 的总和

其中:

  • n 是空闲块的总数
  • FreeSlots(i) 表示可用空闲内存空间中可以容纳多少个 i 大小的插槽
  • IdealFreeSlots(i) 表示有多少 i 大小的插槽适合大小为 n 的完美未碎片内存。这是一个简单的计算:IdealFreeSlots(i) = Floor(n / i)

我是如何想出这个公式的:

我正在考虑如何组合所有 freespace_quality(i) 值来获得单个碎片百分比,但我对此函数的结果不是很满意。即使在理想情况下,如果可用空间大小 n 不能被 i 整除,您也可能会得到 freespace_quality(i) != 1。例如,如果 n=10i=3,则 freespace_quality(3) = 9/10 = 0.9

因此,我创建了一个派生函数 freespace_relative_quality(i) ,如下所示:

< img src="https://i.sstatic.net/2qHVc.gif" alt="freespace_relative_quality(i) = freespace_quality(i) / Ideal_freespace_quality(i)">

这总是有输出 1 在理想的“完全不碎片化”的场景中。

计算后:

在此处输入图像描述

要得出最终碎片公式,现在剩下要做的就是计算所有 i 值的平均自由空间质量(来自 1n),然后通过执行 1 - 平均质量 反转范围,这样 0 表示完全未碎片(最高质量),1 表示碎片最多(最低质量)。

Based on R.. GitHub STOP HELPING ICE's answer, I came up with the following way of computing fragmentation as a single percentage number:

Fragmentation = 1 - 1/n * sum with i=1..n of FreeSlots(i)/IdealFreeSlots(i)

Where:

  • n is the total number of free blocks
  • FreeSlots(i) means how many i-sized slots you can fit in the available free memory space
  • IdealFreeSlots(i) means how many i-sized slots would fit in a perfectly unfragmented memory of size n. This is a simple calculation: IdealFreeSlots(i) = floor(n / i).

How I came up with this formula:

I was thinking about how I could combine all the freespace_quality(i) values to get a single fragmentation percentage, but I wasn't very happy with the result of this function. Even in an ideal scenario, you could have freespace_quality(i) != 1 if the free space size n is not divisible by i. For example, if n=10 and i=3, freespace_quality(3) = 9/10 = 0.9.

So, I created a derived function freespace_relative_quality(i) which looks like this:

freespace_relative_quality(i) = freespace_quality(i) / ideal_freespace_quality(i)

This would always have the output 1 in the ideal "perfectly unfragmented" scenario.

After doing the math:

enter image description here

All that's left to do now to get to the final fragmentation formula is to calculate the average freespace quality for all values of i (from 1 to n), and then invert the range by doing 1 - the average quality so that 0 means completely unfragmented (maximum quality) and 1 means most fragmented (minimum quality).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文