内存碎片
2014-04-27 02:25 youxin 阅读(1190) 评论(0) 编辑 收藏 举报In computer storage, fragmentation is a phenomenon in which storage space is used inefficiently, reducing capacity or performance and often both. The exact consequences of fragmentation depend on the specific system of storage allocation in use and the particular form of fragmentation. In many cases, fragmentation leads to storage space being "wasted", and in that case the term also refers to the wasted space itself. For other systems (e.g. the FAT file system) the space used to store given data (e.g. files) is the same regardless of the degree of fragmentation (from none to extreme).很多情况下,碎片导致空间被浪费。
There are three different but related forms of fragmentation: external fragmentation, internal fragmentation, and data fragmentation, which can be present in isolation or conjunction. Fragmentation is often accepted in return for improvements in speed or simplicity.
外部碎片,内部碎片,数据碎片。
basic principle:
When a computer program requests blocks of memory from the computer system, the blocks are allocated in chunks. When the computer program is finished with a chunk, it can free the chunk back to the system, making it available to later be allocated again to another or the same program. The size and the amount of time a chunk is held by a program varies. During its lifespan, a computer program can request and free many chunks of memory.
When a program is started, the free memory areas are long and contiguous. Over time and with use, the long contiguous regions become fragmented into smaller and smaller contiguous areas. Eventually, it may become impossible for the program to obtain large contiguous chunks of memory.
当一个计算机程序从计算机内粗里获取一些block时,blocks都会以chunk的方式分配,当这个程序使用完了相应的空间之后,它将被释放回计算机。这份内存空间会被持有的时间由该程序决定(每个程序都是不一样的)。在这个程序的生命周期内,获取和释放多个内存空间。(内存碎片就是在这个过程中产生的)
当一个程序刚启动的时候,它的空闲内存区域是线性连续的。随着程序运行,连续的区域开始被打碎成小的连续区域。最终,这个程序不能再有效获取到足够大的内存区域。
Internal fragmentation
Due to the rules governing memory allocation, more computer memory is sometimes allocated than is needed. For example, memory can only be provided to programs in chunks divisible by 4, 8 or 16, and as a result if a program requests perhaps 23 bytes, it will actually get a chunk of 32 bytes. When this happens, the excess memory goes to waste. In this scenario, the unusable memory is contained within an allocated region. This arrangement, termed fixed partitions, suffers from inefficient memory use - any process, no matter how small, occupies an entire partition. This waste is called internal fragmentation.[1][2]
Unlike other types of fragmentation, internal fragmentation is difficult to reclaim; usually the best way to remove it is with a design change. For example, indynamic memory allocation, memory pools drastically cut internal fragmentation by spreading the space overhead over a larger number of objects.
g根据内存分配的规则,许多计算机内存有时分配的比程序需要的多。例如,程序申请的内存只能被4,8,16整除,如果一个程序想申请
23Byte,最终会得到32Byte。结果,多余的内存浪费了。
不像其他形式的碎片,内部碎片很难发现,通常最好的方式移除他是重新设计。