A-A+

关于linux swap-添加/删除/手动清空

2013年05月20日 Linux 暂无评论 阅读 8,103 次

关于linux swap
Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释 放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到 内存中。这样,系统总是在物理内存不够时,才进行Swap交换。
再有,即使你的机器拥有足够多的物理内存,也有一些程序会在它们初始化时残留的极少再用到的内存分页内容转移到 swap 空间,以此让出物理内存空间。

如果在使用过程中想要清空 swap 空间,可以先禁用 swap,然后再次启用。以linux为例:

[oracle@ora10gr2 ~]$ top

top - 23:58:15 up 6 min,  1 user,  load average: 0.03, 0.20, 0.11
Tasks: 106 total,   1 running, 105 sleeping,   0 stopped,   0 zombie
Cpu(s):  3.2%us,  3.3%sy,  0.0%ni, 78.4%id, 10.5%wa,  4.1%hi,  0.4%si,  0.0%st
Mem:   2075468k total,   520284k used,  1555184k free,    17080k buffers
Swap:  4128696k total,        0k used,  4128696k free,   401756k cached

[root@ora10gr2 ~]# /sbin/swapoff -a
[oracle@ora10gr2 ~]$ top

top - 00:00:19 up 9 min,  2 users,  load average: 0.00, 0.12, 0.09
Tasks: 114 total,   2 running, 112 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.3%sy,  0.0%ni, 93.3%id,  1.3%wa,  4.3%hi,  0.7%si,  0.0%st
Mem:   2075468k total,   536072k used,  1539396k free,    17212k buffers
Swap:        0k total,        0k used,        0k free,   413568k cached

oracle正常

SQL> select * from gv$version;

   INST_ID BANNER
---------- ----------------------------------------------------------------
         1 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
         1 PL/SQL Release 10.2.0.1.0 - Production
         1 CORE 10.2.0.1.0      Production
         1 TNS for Linux: Version 10.2.0.1.0 - Production
         1 NLSRTL Version 10.2.0.1.0 - Production


[root@ora10gr2 ~]# /sbin/swapon -a
[oracle@ora10gr2 ~]$ top

top - 00:01:01 up 9 min,  2 users,  load average: 0.00, 0.11, 0.09
Tasks: 114 total,   1 running, 113 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.4%us,  2.4%sy,  0.0%ni, 82.6%id,  8.0%wa,  4.2%hi,  0.4%si,  0.0%st
Mem:   2075468k total,   538544k used,  1536924k free,    17280k buffers
Swap:  4128696k total,        0k used,  4128696k free,   413920k cached

扩容/创建swap空间

[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026        551       1475          0         17        430
-/+ buffers/cache:        103       1922
Swap:         4031          0       4031
[root@ora10gr2 ~]# dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 28.3604 seconds, 18.9 MB/s
[root@ora10gr2 ~]# mkswap /mnt/512Mb.swap
Setting up swapspace version 1, size = 536866 kB
[root@ora10gr2 ~]# swapon /mnt/512Mb.swap
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026       1071        955          0         18        942
-/+ buffers/cache:        110       1916
Swap:         4543          0       4543

写入到fstab中
/mnt/512Mb.swap none swap sw 0 0

删除swap分区

[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026       1071        955          0         18        942
-/+ buffers/cache:        110       1916
Swap:         4543          0       4543
[root@ora10gr2 ~]# swapoff /mnt/512Mb.swap
[root@ora10gr2 ~]# rm -rf /mnt/512Mb.swap
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026        544       1482          0         18        430
-/+ buffers/cache:         95       1931
Swap:         4031          0       4031

清楚fstab中的记录

唯一的swap理论上也是可以在线删的

[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026       1063        962          0         19        942
-/+ buffers/cache:        102       1924
Swap:         4031          0       4031
[root@ora10gr2 ~]# cat /etc/fstab 
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda3         swap                    swap    defaults        0 0
[root@ora10gr2 ~]# swapoff LABEL=SWAP-sda3
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026        543       1483          0         19        430
-/+ buffers/cache:         93       1933
Swap:            0          0          0
[root@ora10gr2 ~]# swapon LABEL=SWAP-sda3
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026        569       1457          0         19        455
-/+ buffers/cache:         94       1932
Swap:         4031          0       4031

最好是创建一个新的替换之,也就是手动清空swap空间(始终保证系统存在一个可用的swap)

[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026        569       1457          0         19        455
-/+ buffers/cache:         94       1932
Swap:         4031          0       4031
[root@ora10gr2 ~]# dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 7.62524 seconds, 70.4 MB/s
[root@ora10gr2 ~]# mkswap /mnt/512Mb.swap
Setting up swapspace version 1, size = 536866 kB
[root@ora10gr2 ~]# swapon /mnt/512Mb.swap
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026       1090        936          0         20        967
-/+ buffers/cache:        102       1924
Swap:         4543          0       4543
[root@ora10gr2 ~]# cat /etc/fstab 
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda3         swap                    swap    defaults        0 0
[root@ora10gr2 ~]# swapoff LABEL=SWAP-sda3
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026       1088        938          0         20        967
-/+ buffers/cache:        100       1926
Swap:          511          0        511
[root@ora10gr2 ~]# swapon LABEL=SWAP-sda3
[root@ora10gr2 ~]# swapoff /mnt/512Mb.swap
[root@ora10gr2 ~]# rm -rf /mnt/512Mb.swap
[root@ora10gr2 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2026        571       1455          0         20        455
-/+ buffers/cache:         95       1931
Swap:         4031          0       4031

查看哪个进程在使用swap
top下,按f,按p标记swap出现在top输出中,按P按cpu排序,按M按内存排序。

给我留言

Copyright © YallonKing 保留所有权利.   Theme  Ality

用户登录

分享到: