zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Centos修改swap虚拟内存大小方法

centos方法 修改 大小 swap 虚拟内存
2023-06-13 09:15:57 时间
[root@gamedb ~]# free –m

[root@gamedb tmp]# dd if=/dev/zero of=/tmp/big_swap bs=1024 count=2000000

[root@gamedb tmp]# du -sh /tmp/big_swap

[root@gamedb tmp]# mkswap /tmp/big_swap

[root@gamedb tmp]# swapon /tmp/big_swap

[root@gamedb tmp]# free –m

[root@gamedb tmp]# vi /etc/fstab

# LABEL=SWAP-hda2 swap swap defaults 0 0

 /tmp/big_swap swap swap defaults 0 0

1、查看内存和swap大小,发现swap是内存大小的一半

[root@localhost tmp]# free -m

 total used free shared buffers cached

Mem: 772 91 681 0 6 30

-/+ buffers/cache: 53 718

Swap: 1567 0 1567

2、决定修改swap大小,首先在空间合适处创建用于分区的swap文件:

[root@localhost tmp]# dd if=/dev/zero of=/tmp/big_swap bs=1024 count=2000000 

记录了2000000+0 的读入

记录了2000000+0 的写出

2048000000字节(2.0 GB)已复制,69.5342 秒,29.5 MB/秒

(注意:if 表示 infile,of 表示outfile,bs=1024 表示写入的每个块的大小为1024B=1KB(1024B字节=1024*8bit位)

3、查看创建的文件大小是否符合要求:

[root@localhost tmp]# du -sh big_swap 

2.0G big_swap

[root@localhost tmp]# ls -al big_swap

-rw-r--r-- 1 root root 2048000000 12月 1 14:25 big_swap

4、将目的文件设置为swap分区文件:

[root@localhost tmp]# mkswap big_swap 

mkswap: big_swap: warning: dont erase bootbits sectors

 on whole disk. Use -f to force.

Setting up swapspace version 1, size = 1999996 KiB

no label, UUID=25c3eaf2-43f9-487b-87b7-f76a712f5376

5、激活swap,立即启用交换分区文件:

[root@localhost tmp]# swapon big_swap

6、再次查看内存和虚拟内存,发现已经被正常修改:

[root@localhost tmp]# free -m

 total used free shared buffers cached

Mem: 772 705 66 0 2 627

-/+ buffers/cache: 75 697

Swap: 3521 0 3521

7、若要想使开机时自启用,则需修改文件/etc/fstab中的swap行:

[root@gamedb tmp]# vi /etc/fstab

# LABEL=SWAP-hda2 swap swap defaults 0 0

 /tmp/big_swap swap swap defaults 0 0

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/57625.html