Linux的df和du命令的区别
备注:工作中遇到如下一个bug:
camera录制一段视频,从“File Manager”删除最后录制以后,发现闪存的大小没变化。df查看disk space 没有被释放,du查看的话:/mnt/sdcard/DCIM/Camera文件夹大小有变化。
在网上,查了下df和du命令的区别,果然跟最后debug的结果一样。
9.24. The du and df commands show different amounts of disk space available. What is going on?
You need to understand what du and df really do. du goes through the directory tree, measures how large each file is, and presents the totals. df just asks the file system how much space it has left. They seem to be the same thing, but a file without a directory entry will affect df but not du.
When a program is using a file, and you delete the file, the file is not really removed from the file system until the program stops using it. The file is immediately deleted from the directory listing, however. You can see this easily enough with a program such as more. Assume you have a file large enough that its presence affects the output of du and df. (Since disks can be so large today, this might be a very large file!) If you delete this file while using more on it, more does not immediately choke and complain that it cannot view the file. The entry is simply removed from the directory so no other program or user can access it. du shows that it is gone -- it has walked the directory tree and the file is not listed. df shows that it is still there, as the file system knows that more is still using that space. Once you end the more session, du and df will agree.
Note that Soft Updates can delay the freeing of disk space; you might need to wait up to 30 seconds for the change to be visible!
This situation is common on web servers. Many people set up a FreeBSD web server and forget to rotate the log files. The access log fills up /var. The new administrator deletes the file, but the system still complains that the partition is full. Stopping and restarting the web server program would free the file, allowing the system to release the disk space. To prevent this from happening, set up newsyslog(8).
du就是通过搜索文件来计算每个文件的大小然后累加
df通过文件系统来快速获取空间大小的信息
1、句柄相关的基础知识
句柄包含:文件、目录、socket等
一个进程能够打开的文件句柄总数是一定的,可以通过ulimit –n 进行查看,一般是1024个。
此参数也可以修改,以root用户运行以下命令:ulimit -HSn 4096 以上命令中,H指定了硬性大小,
S指定了软性大小,n表示设定单个进程最大的打开文件句柄数量。
个人觉得最好不要超过4096,毕竟打开的文件句柄数越多响应时间肯定会越慢。设定句柄数量后,
系统重启后,又会恢复默认值。
如果想永久保存下来,可以修改.bash_profile文件,可以修改 /etc/profile ,把命令ulimit -HSn 4096
加到最后。
2、句柄泄漏的危害
出现句柄泄漏,进程会逐渐消耗完进程可打开的句柄,导致进程再也不能打开新的句柄,系统可能不能再正常工作。
3、句柄泄露,为什么会导致磁盘空间不足::
一个文件创建,就新生成了一个inode节点,且inode节点的引用计数为1。当文件被一个进程打开,
则在内存中缓冲建立此inode节点缓存,且其引用计数加1。一个文件可以被多次打开,创建多个文件描述符,
但对应的inode节点只有一个,对应的磁盘存储只有一个,只是引用计数每打开一次加1。
当文件被rm,在操作系统内部其实是调用了unlink操作,其目录项被删除,
如果其对应的inode节点的引用计数为0,则删除对应的inode节点和清除inode节点位图中对应的位,
其对应磁盘可被再申请;
如果此文件被其他进程打开,则其对应的inode节点的引用计数不为0,则不会删除对应的inode
节点和清除inode节点位图中对应的位,其对应磁盘不能被再申请,也就是在lsof下可以看到deleted句柄;
当前文件被所有使用进程close,系统删除对应的inode节点和清除inode节点位图中对应的位,
其对应磁盘可被再申请。