Based on R.. GitHub STOP HELPING ICE's answer, I came up with the following way of computing fragmentation as a single percentage number:
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:
This would always have the output 1
in the ideal "perfectly unfragmented" scenario.
After doing the math:
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).