Linux碎片整理工具,Linux不需要磁盘碎片整理

今天老师上课时谈到过Windows的磁盘碎片整理。以前就知道Linux一般不需要进行磁盘碎片整理。但是考虑到我还有3个NTFS分区,看可不可以在Linux下对其进行整理。于是便去google……结果发现,没有那样的软件。

算了,没有就没有。反正NTFS分区里放的都是一些影音娱乐文件。无所谓咯……

下面是Ubuntu中文论坛找到的一篇关于解释Linux不需要进行磁盘碎片整理的帖子。大家有兴趣可以看下。

——————————————华丽的分割线—————————————————

引用:

10.4. Some facts about file systems and fragmentation

Disk space is administered by the operating system in units of

blocks and fragments of blocks. In ext2, fragments and blocks have

to be of the same size, so we can limit our discussion to

blocks.

Files come in any size. They don't end on block boundaries. So with

every file a part of the last block of every file is wasted.

Assuming that file sizes are random, there is approximately a half

block of waste for each file on your disk. Tanenbaum calls this

"internal fragmentation" in his book "Operating Systems".

You can guess the number of files on your disk by the number of

allocated inodes on a disk. On my disk

# df -i

Filesystem Inodes IUsed IFree %IUsed Mounted on

/dev/hda3 64256 12234 52022 19% /

/dev/hda5 96000 43058 52942 45% /var

there are about 12000 files on / and about 44000 files on /var. At

a block size of 1 KB, about 6+22 = 28 MB of disk space are lost in

the tail blocks of files. Had I chosen a block size of 4 KB, I had

lost 4 times this space.

Data transfer is faster for large contiguous chunks of data,

though. That's why ext2 tries to preallocate space in units of 8

contigous blocks for growing files. Unused preallocation is

released when the file is closed, so no space is wasted.

Noncontiguous placement of blocks in a file is bad for performance,

since files are often accessed in a sequential manner. It forces

the operating system to split a disk access and the disk to move

the head. This is called "external fragmentation" or simply

"fragmentation" and is a common problem with MS-DOS file systems.

In conjunction with the abysmal buffer cache used by MS-DOS, the

effects of file fragmentation on performance are very noticeable.

DOS users are accustomed to defragging their disks every few weeks

and some have even developed some ritualistic beliefs regarding

defragmentation.

None of these habits should be carried over to Linux and ext2.

Linux native file systems do not need defragmentation under normal

use and this includes any condition with at least 5% of free space

on a disk. There is a defragmentation tool for ext2 called defrag,

but users are cautioned against casual use. A power outage during

such an operation can trash your file system. Since you need to

back up your data anyway, simply writing back from your copy will

do the job.

The MS-DOS file system is also known to lose large amounts of disk

space due to internal fragmentation. For partitions larger than 256

MB, DOS block sizes grow so large that they are no longer useful

(This has been corrected to some extent with FAT32). Ext2 does not

force you to choose large blocks for large file systems, except for

very large file systems in the 0.5 TB range (that's terabytes with

1 TB equaling 1024 GB) and above, where small block sizes become

inefficient. So unlike DOS there is no need to split up large disks

into multiple partitions to keep block size down.

Use a 1Kb block size if you have many small files. For large

partitions, 4Kb blocks are fine.

希望有能力、有闲暇地朋友能对上面的官方材料进行翻译,我的能力有所不及,这里仅仅做一些阐述。

这段linux官方资料主要介绍了外部碎片(external fragmentation)、内部碎片(internal

fragmentation)的概念及相关情况,说明了linux文件系统在磁盘还有5%空闲空间的情况下是不需要碎片整理的。(Linux

native file systems do not need defragmentation under normal use

and this includes any condition with at least 5% of free space on a

disk.)。而在实际使用中,磁盘在还有8%左右未使用时就会有警告产生,所以碎片整理是不用考虑的。

产生碎片整理想法的主要在两类朋友中,一类是受windows思想影响的朋友,还有一类是对操作系统原理有一定程度了解的朋友。

我在这里先简单地说明一些问题。

所有地操作系统都会产生磁盘碎片,这正是某些朋友产生疑虑的原因。这个碎片在上面地官方资料中称为内部碎片。它是这样产生的,假设一个磁盘的空间有20k,它的基本存储单位为簇,设有两个文件,一个7k,一个1k。当簇的大小为4k时,磁盘分为了5个簇,两个文件共占用3个簇,即使用了12k,其中浪费地空间就是4k,也就是产生了内部碎片4k。因此我们就了解了:内部碎片主要是造成磁盘空间的浪费。请注意:windows的磁盘碎片整理功能所整理的碎片不是这个碎片,也无法对这个碎片进行操作,它所对应的碎片概念是外部碎片。

那么,可以对内部碎片进行优化处理吗?答案是肯定的。以上面的例子来说,如果把每一簇分成2k,那么20k的磁盘就分为了10个簇,7k和1k两个文件共占用了5个簇,10k的空间,浪费的空间,即内部碎片为2k。

由此可见,簇分的越小,所浪费的空间越少。这也是NTFS比FAT32优秀的一个地方。在Win

2000的FAT32文件系统的情况下,分区大小在2GB~8GB时簇的大小为4KB;分区大小在8GB~16GB时簇的大小为8KB;分区大小在

16GB~32GB时,簇的大小则达到了16KB。而Win

2000的NTFS文件系统,当分区的大小在2GB以下时,簇的大小都比相应的FAT32簇小;当分区的大小在2GB以上时(2GB~2TB),簇的大小

都为4KB。相比之下,NTFS可以比FAT32更有效地管理磁盘空间,最大限度地避免了磁盘空间的浪费。

有的朋友会进一步的思考,那么为什么文件系统不是把簇分的非常的小呢?这里就引出了另一个问题,文件访问查找的问题。还是以上面的例子说明,当我们要查找使用一个文件时,就需要通过页表来进行访问。打个比方,你住的地方就好比是文件所占用的簇,但是要找到你,就得通过你的住址来进行访问,而访问文件则是通过文件分配表。如果住的人多,地址也就很多,那么要查到你住的地址所花的时间也就很多。同样的道理,当簇分的越小,记录簇的地址也就越大,查找文件所在的簇所花的时间也就越多。当簇为4k时,簇的地址是5个,而簇为2k时,簇的地址是10个。因而簇的大小是在空间和时间上取得平衡的一个结果。

这里也对另一个问题作一些提示,有些第三方分区软件可以自定义簇的大小,建议采用默认值,否则会在某些情况下产生一些问题。

有的朋友会进一步提问:那么为什么在普通情况下NTFS分的簇会比FAT32的要小,而访问速度会差不多呢?这又牵涉到文件访问机制等等问题。这里我就不再介绍了,其实这个问题我也不能完全说清,有兴趣的朋友可以找一些操作系统方面的资料进行阅读,可以在一定程度上解决这个问题。

好,下面开始我们的重点:linux不需要碎片整理!

windows概念下的碎片,在上面linux官方资料中称为外部碎片,它就是影响性能的那个碎片概念。(This is called

"external fragmentation" or simply "fragmentation" and is a common

problem with MS-DOS file systems.

)而linux一般不会产生这种碎片。外部磁盘碎片应该称为文件碎片,是因为文件被分散保存到整个磁盘的不同地方,而不是连续地保存在磁盘连续的簇中形成的。

当应用程序所需的物理内存不足时,一般操作系统会在硬盘中产生临时交换文件,用该文件所占用的硬盘空间虚拟成内存。虚拟内存管理程序会对硬盘频繁读写,产生大量的碎片,这是产生硬盘碎片的主要原因。

其他如IE浏览器浏览信息时生成的临时文件或临时文件目录的设置也会造成系统中形成大量的碎片。文件碎片一般不会在系统中引起问题,但文件碎片

过多会使系统在读文件的时候来回寻找,引起系统性能下降,严重的还要缩短硬盘寿命。另外,过多的磁盘碎片还有可能导致存储文件的丢失。

上面所说的就是windows如何产生外部碎片的,其实这与文件系统所使用的数据结构有关。对于FAT来说,使用的是chain式的结构来记录一个文件所使用的簇。这种方式的好处就是有助于文件的动态增长的需要。但是却带了碎片的问题,使得读写文件的时候,磁头频繁移动。对于CD-ROM,由于是

read-only的,所以不存在数据增长的问题,所以,采用了连续的方法来记录数据,也不会产生碎片,而linux的ext等文件格式与CD-ROM的存储有相似之处。

下面这篇文章通俗易懂地解说了为什么linux不需要碎片整理以及windows为什么需要碎片整理:

来自http://geekblog.oneandoneis2.org/index.php/2006/08/17/why_doesn_t_linux_need_defragmenting

请注意,官方资料所说的是linux文件系统在磁盘还有5%空闲空间的情况下是不需要碎片整理的。(Linux native file

systems do not need defragmentation under normal use and this

includes any condition with at least 5% of free space on a

disk.)。而在实际使用中,磁盘在还有8%左右未使用时就会有警告产生,所以碎片整理是不用考虑的。

而下文中说的是20%。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值