Linux磁盘与内存清理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36274515/article/details/86804403

Linux内存和磁盘清理

一、磁盘清理

1. 三种常见的磁盘清理方式

# 清理旧版本的软件缓存
sudo apt-get autoclean
# 清理所有软件缓存
sudo apt-get clean 
# 删除系统不再使用的孤立软件
sudo apt-get autoremove 

2. 清理opera/ firefox的缓存文件

ls ~/.opera/cache4
ls ~/.mozilla/firefox/*.default/Cache

3. 清理Linux下孤立的包

# 图形界面下我们可以用:gtkorphan
sudo apt-get install gtkorphan -y
# 终端命令下我们可以用:deborphan
sudo apt-get install deborphan -y

4. 卸载:tracker

这个东西一般我只要安装Ubuntu就会第一删掉**tracker** 他不仅会产生大量的cache文件而且还会影响开机速度。所以在新得利里面删掉就行。

5. 删除多余的内核

# 打开终端敲命令:
dpkg --get-selections|grep linux

有image的就是内核文件

删除老的内核文件:
sudo apt-get remove 内核文件名 
# 例如:linux-image-2.6.27-2-generic

内核删除,释放空间了,应该能释放130-140M空间。
最后不要忘了看看当前内核:
uname -a

附录:
包管理的临时文件目录:
包在 : /var/cache/apt/archives
没有下载完的在 : /var/cache/apt/archives/partial

6. 直接查找大文件

# 在当前目录查看最大文件
du -h --max-depth=1
# 查看当前目录下各文件及文件夹占用大小
du -sh *

二、清理内存

大家都知道,Linux服务器为了提高效率,会提前申请内存,即使这些内存没有被具体应用使用,Linux也会提前申请这些内存,然后利用这些内存做缓存用,即将刚打开的文件系统存入cache中,这样对应的服务器free值会越来越少,buffers和cached会越来越大,因此给大家表象就是内存越来越少了,大家就紧张了;其实,大家完全不用紧张,Linux服务器在发现内存不足时,会自动清理cached区域,释放内存,然后继续增大cache,free继续减少。因此,那样手动降低内存使用率的方法,其实就是图一时之快。

1. Linux内存解释

free -m # 以直观方式查看内存使用情况

total——总物理内存
used——已使用内存,一般情况这个值会比较大,因为这个值包括了cache+应用程序使用的内存
free——完全未被使用的内存
shared——应用程序共享内存
buffers——缓存,主要用于目录方面,inode值等(ls大目录可看到这个值增加)
cached——缓存,用于已打开的文件

note:
    total=used+free
    used=buffers+cached (maybe add shared also)

第二行描述应用程序的内存使用:
前个值表示-buffers/cache——应用程序使用的内存大小,used减去缓存值
后个值表示+buffers/cache——所有可供应用程序使用的内存大小,free加上缓存值

note:
       -buffers/cache=used-buffers-cached
       +buffers/cache=free+buffers+cached

第三行表示swap的使用:

    used —— 已使用
    free —— 未使用

2. 执行sync命令

使用**sync**命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件。

3. 修改/proc/sys/vm/drop_caches

echo 3 > /proc/sys/vm/drop_caches

说明:

  1. /proc是一个虚拟文件系统,我们可以通过对它的读写操作作为与kernel实体间进行通信的一种手段。也就是说可以通过修改/proc中的文件,来对当前kernel的行为做出调整。也就是说我们可以通过调整/proc/sys/vm/drop_caches来释放内存。
  2. 关于drop_caches的官方说明如下:

Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to becomefree.

To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.

Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.

  1. Linux内核会将它最近访问过的文件页面缓存在内存中一段时间,这个文件缓存被称为pagecache。

猜你喜欢

转载自blog.csdn.net/qq_36274515/article/details/86804403
今日推荐