0%

vps内存优化之清理内存

前言

vps的内存本身不大,缓存就占了1.4G,必须得清理了。

优化内存的方法

过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@vultr:~# free -mh
total used free shared buff/cache available
Mem: 1.9G 197M 357M 752K 1.4G 1.6G
Swap: 0B 0B 0B
root@vultr:~# sync; echo 1 > /proc/sys/vm/drop_caches
root@vultr:~# free -mh
total used free shared buff/cache available
Mem: 1.9G 196M 1.5G 752K 267M 1.6G
Swap: 0B 0B 0B
root@vultr:~# sync; echo 2 > /proc/sys/vm/drop_caches
root@vultr:~# free -mh
total used free shared buff/cache available
Mem: 1.9G 193M 1.6G 752K 166M 1.6G
Swap: 0B 0B 0B

说明:

  • 仅清除页面缓存(PageCache)
    1
    sync; echo 1 > /proc/sys/vm/drop_caches
  • 清除目录项和inode
    1
    sync; echo 2 > /proc/sys/vm/drop_caches
  • 清除页面缓存,目录项和inode
    1
    sync; echo 3 > /proc/sys/vm/drop_caches

创建crontab定期清理缓存

1
2
root@vultr:~# vi clearcache.sh
root@vultr:~# chmod 755 clearcache.sh

脚本内容:

1
2
#!/bin/bash
echo "echo 1 > /proc/sys/vm/drop_caches"

创建crontab:

1
2
3
4
5
6
7
8
9
10
root@vultr:~# crontab -e
no crontab for root - using an empty one

Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /bin/ed

Choose 1-4 [1]: 3

crontab的内容:

1
0 3 * * * /root/clearcache.sh

参考

参考这篇文章

-------------本文结束感谢您的阅读-------------