swap分区
swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用,那些被释放的
空间可能来自一些很长时间没有操作的程序,这些被释放的空间被临时保存到swap分区中,等到那些程序要运行时,再从SWAP
分区中恢复保存的数据到内存中。
swap配置对性能的影响
分配太多的Swap空间会浪费磁盘空间,而Swap空间太小,则系统会发生错误。
如果系统的物理内存用光了,系统就会跑得很慢,单仍然能运行,如果swap用完,那么系统会的发生错误,例如,web服务器能
根据不同的请求衍生出多个服务进程(或线程),如果swap空间用完,则服务器无法启动,通常还会出现"application is
out of memory"的错误,严重时会造成服务进程死锁。因此swap空间分配时很重要。
通常情况下,swap空间应大于或等于物理内存的大小,最小不应小于64M,通常swap大小应是物理内存的2-2.5陪。
但根据不同的应用,应有不同的配置:如果是小的桌面系统,则只需要较小的swap空间,而大的服务器系统则视情况不
同需要不同大小的swap空间。贴别是数据库服务器和web服务器,随着访问量的增加,对swap空间的要求也会增加,具
体配置参见各服务器产品的说明.
另外,swap分区的数量对性能也有很大的影响,因为swap交换的操作是磁盘io的操作,如果有多个swap交换区,sw
ap空间分配会已轮流的方式操作于swap,这样大大均衡IO的负载。加快swap的交换速度。如果只有一个交换区,所有的
交换操作会使交换区变得很忙,使系统大多时间处于等待状态,效率很低。用性能监视工具就会发现,CPU并不忙,而系
统却慢。这说明瓶颈在IO上,依靠提高CPU的速度是解决不了问题的.
Swap的负载保持在30%以下,这样才保证系统良好性能.
增加/删除Swap分区空间方法
一)、 通过增加一个swap文件;
[root@localhost Desktop]# free
total used free shared buffers cached
Mem: 873436 744120 129316 0 55520 334260
-/+ buffers/cache: 354340 519096
Swap: 1769464 8 1769456
[root@localhost Desktop]# dd if=/dev/zero of=/swp/swapfile bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.576356 s, 364 MB/s
mkswap /swp/swapfile
[root@localhost Desktop]# mkswap /swp/swapfile -----标记为swap
mkswap: /swp/swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 204796 KiB
no label, UUID=13f11ec9-e1bf-40bd-b7b6-f45fc544ce8f
[root@localhost Desktop]# swapon /swp/swapfile ---激活
最后一步自动挂载
vim /etc/fstab
/swp/swapfile swap swap defaults 0 0 ------加上这行
swapon -a 启用
swapon -s 查看成功启用
二)、增加一个SWAP分区
[root@localhost Desktop]# fdisk /dev/sdc ----创建一个分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x8e6383ef.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): p
Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8e6383ef
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +200M
Command (m for help): p
Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8e6383ef
Device Boot Start End Blocks Id System
/dev/sdc1 1 26 208813+ 83 Linux
Command (m for help): t -------------更改类型
Selected partition 1
Hex code (type L to list codes): 82 ---------------82为linux swap
Changed system type of partition 1 to 82 (Linux swap / Solaris)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost Desktop]# mkswap -c -v1 /dev/sdc1 -----建立一个Linux的交换区 -c 参数为 在创建交换区之前,检测块设备是否有坏道。
Setting up swapspace version 1, size = 208808 KiB
no label, UUID=3d9979e2-e2df-44c8-bfcd-cf4fef5458f7
swapon /dev/sdc1 ------激活swap分区
vim /etc/fstab 开启自动挂载
/dev/sdc1 swap swap defaults 0 0
关闭swapoff /dev/sdc
然后swapon -a
swapon -s 查看是否成功挂载
[root@localhost Desktop]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 1769464 0 -1
/swp/swapfile file 204792 0 -2
/swp/swapfile1 file 204792 0 -3
/dev/sdc1 partition 208804 0 -4 -------------------显示出来 代表设置成功
end-------------------如有错误,欢迎留言!