修改dirty_bytes(脏位),使得linux的文件管理器nautilus复制文件的进度显示准确

有时在linux中的文件浏览器(例如nautilus,下面用此举例)中复制或者移动文件,会发现进度条很快就完成了,显示剩余0s,但是却迟迟不显示操作成功。

原因是当nautilus在处理写入操作时,linux内核把排队等待写入硬盘的数据先保存到内存缓冲区,然后提示“已完成”,于是nautilus就会认为已经复制完毕了,但其实只是写入到了内存缓冲区。这时,nautilus想要关闭被复制中的文件,但是内核会阻止nautilus,而把缓冲区的信息写入硬盘。这样nautilus不知道缓冲区的信息写入硬盘要多久,会一直等下去,所以我们看到一直显示剩余0s,nautilus等到内核写完后才能显示已完成。

解决办法是,可以修改/proc/sys/vm/dirty_bytes来减少内核使用的内存缓冲区,比如把dirty_bytes(脏位)改成15728640,即15MB。这样,程序就不会误差太多,比如nautilus的进度显示就不会误差超过15MB。

但是这样可能会导致写入操作吞吐量降低。不过,把脏位改低能够预防在内存不足时系统无响应。但不要把改得太低,可以预估一下硬盘的写入量,然后改成适合的值。

关于脏位的介绍
dirty_bytes

Contains the amount of dirty memory at which a process generating disk writes
will itself start writeback.

Note: dirty_bytes is the counterpart of dirty_ratio. Only one of them may be
specified at a time. When one sysctl is written it is immediately taken into
account to evaluate the dirty memory limits and the other appears as 0 when
read.

Note: the minimum value allowed for dirty_bytes is two pages (in bytes); any
value lower than this limit will be ignored and the old configuration will be
retained.

你可能感兴趣的:(Linux)