第17章 空闲空间管理

Homework

The program, malloc.py, lets you explore the behavior of a simple free-space allocator as described in the chapter. See the README for details of its basic operation.

Questions

1. First run with the flags -n 10 -H 0 -p BEST -s 0 to generate a few random allocations and frees. Can you predict what alloc()/free() will return? Can you guess the state of the free list after each request? What do you notice about the free list over time?

题目要求:

5次内存申请操作,头结构大小为 0,利用最优匹配算法管理空闲列表 ,随机数种子为0

可以观察到,因为不支持合并操作,在多次申请并释放内存后,内存碎片化很严重

python malloc.py -n 10 -H 0 -p BEST -s 0 -c

2. How are the results different when using a WORST fit policy to search the free list (-p WORST)? What changes?

相比最优匹配,最差匹配使得内存碎片化更严重 

python malloc.py -n 10 -H 0 -p WORST -s 0 -c

3. What about when using FIRST fit (-p FIRST)? What speeds up when you use first fit? 

首次匹配算法的运行速度很快,因为不用像最优匹配或者最差匹配一样遍历整个空闲列表

python malloc.py -n 10 -H 0 -p FIRST -s 0 -c

4. For the above questions, how the list is kept ordered can affect the time it takes to find a free location for some of the policies. Use the different free list orderings (-l ADDRSORT, -l SIZESORT+, -l SIZESORT-) to see how the policies and the list orderings interact. 

对于最优匹配,应该采用空闲列表升序排列的策略(就像最小堆),这样可以避免遍历

python malloc.py -n 10 -H 0 -p BEST -l SIZESORT+ -s 0 -c

对于最差匹配,应该采用空闲列表降序排列的策略(就像最大堆),这样可以避免遍历

python malloc.py -n 10 -H 0 -p WORST -l SIZESORT- -s 0 -c

对于首次匹配算法,应避免使用空闲列表降序排列的策略,因为会产生大量的内存碎片

书中建议采用基于地址排序的策略,因为这样有利于内存合并操作 

python malloc.py -n 10 -H 0 -p FIRST -l SIZESORT- -s 0 -c

5. Coalescing of a free list can be quite important. Increase the number of random allocations (say to -n 1000). What happens to larger allocation requests over time? Run with and without coalescing (i.e., without and with the -C flag). What differences in outcome do you see? How big is the free list over time in each case? Does the ordering of the list matter in this case? 

随着增加随机分配的数量(-n 1000),可以模拟一个大型程序运行时对堆的操作, 可以观察到最差匹配的内存碎片化是最严重的

python malloc.py -n 1000 -H 0 -p BEST -c
python malloc.py -n 1000 -H 0 -p WORST -c   
python malloc.py -n 1000 -H 0 -p FIRST -c

 合并可以有效消除内存碎片,只要所有的malloc操作都要对应的free,那内存就可以恢复如初  

python malloc.py -n 1000 -H 0 -p FIRST -C -c

而空闲列表的排序至关重要。

以首次匹配为例子,如果基于地址排序,空闲列表可以很好的合并临近内存,从而消除内存碎片

但是如果按降序或者升序,就会导致内存碎片的增加 

python malloc.py -n 1000 -H 0 -p FIRST -l ADDRSORT -C -c
python malloc.py -n 1000 -H 0 -p FIRST -l SIZESORT+ -C -c
python malloc.py -n 1000 -H 0 -p FIRST -l SIZESORT- -C -c

6. What happens when you change the percent allocated fraction -P to higher than 50? What happens to allocations as it nears 100? What about as the percent nears 0?

-P 接近100时,堆的空间会被快速耗尽,导致后续的malloc操作失败

python malloc.py -n 100 -H 0 -p BEST -P 100 -C -c

-P 接近0时,就是每次malloc后立即free

python malloc.py -n 100 -H 0 -p BEST -P 1 -C -c

7. What kind of specific requests can you make to generate a highly fragmented free space? Use the -A flag to create fragmented free lists, and see how different policies and options change the organization of the free list. 

不free即可

你可能感兴趣的:(操作系统导论,操作系统)