1:创建文件:dd if=/dev/zero  of=/data/swapfile bs=1024 count=65536
2:激活和使用:mkswap /data/swapfile
               free
              swapon /data/swapfile
               free
3:移除:swapoff /data/swapfile
----------------------------------------------------------
上述操作开启重启之后交换空间会变得不可用,需要在/etc/fstab中增加自动挂载设置

/data/swapfile  none  swap sw 0  0



示例:

系统运行过程中一直报错如下

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000d92f0000, 264314880, 0) failed; error='无法分配内存' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 264314880 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/Tomcat_Trans/bin/hs_err_pid10365.log

程序异常中止,查看内存,发现内存配置是一样的(系统迁移),但是竟然报内存不足,free看一下,发现在新系统上没有增加虚拟内存,增加一个2GB的虚拟内存再观察看下,具体如下:




[root@pacteralinux ~]# dd if=/dev/zero  of=/root/swapfile   bs=1024 count=2048000^C
[root@pacteralinux ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             29954008  21352544   7079852  76% /
tmpfs                   794380         0    794380   0% /dev/shm
/dev/sdb1             72246600   4014596  64562092   6% /mnt/resource
/dev/sdc1            379509584    199164 360032424   1% /mnt/resource/mysqldataper
/dev/sdc2            136543368    192128 129415180   1% /mnt/resource/sdc3mnt
[root@pacteralinux ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              29G   21G  6.8G  76% /
tmpfs                 776M     0  776M   0% /dev/shm
/dev/sdb1              69G  3.9G   62G   6% /mnt/resource
/dev/sdc1             362G  195M  344G   1% /mnt/resource/mysqldataper
/dev/sdc2             131G  188M  124G   1% /mnt/resource/sdc3mnt
[root@pacteralinux ~]# touch /mnt/resource/sdc3mnt/swapfile
[root@pacteralinux ~]# dd id=/dev/zero of=/mnt/resource/sdc3mnt/swapfile bs=1024 count=2048000
dd: unrecognized operand `id=/dev/zero'
Try `dd --help' for more information.
[root@pacteralinux ~]# dd if=/dev/zero of=/mnt/resource/sdc3mnt/swapfile bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 134.705 s, 15.6 MB/s
[root@pacteralinux ~]# cd /mnt/resource/sdc3mnt/
[root@pacteralinux sdc3mnt]# ll -h
total 2.0G
drwx------. 2 root root  16K Dec  2 14:20 lost+found
-rw-r--r--. 1 root root 2.0G Dec  7 23:19 swapfile
[root@pacteralinux sdc3mnt]# mkswap swapfile
mkswap: swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=7e85b4b9-5473-4fad-afbe-5b989890e81b
[root@pacteralinux sdc3mnt]# free -m
             total       used       free     shared    buffers     cached
Mem:          1551       1452         99          0          4        635
-/+ buffers/cache:        812        739
Swap:            0          0          0
[root@pacteralinux sdc3mnt]# swapon swapfile
[root@pacteralinux sdc3mnt]# free -m
             total       used       free     shared    buffers     cached
Mem:          1551       1453         97          0          4        635
-/+ buffers/cache:        813        737
Swap:         1999          0       1999
[root@pacteralinux sdc3mnt]#



swapoff问题:swapoff failed: Cannot allocate memory

[root@pacteralinux /]# swapoff /mnt/resource/sdc3mnt/swapfile
swapoff: /mnt/resource/sdc3mnt/swapfile: swapoff failed: Cannot allocate memory

原因:

swapoff执行也是需要内存的,系统没有内存时什么都干不了的

解决:

关闭系统其他进程,然后swapoff

[root@pacteralinux /]# free -m
             total       used       free     shared    buffers     cached
Mem:          1551       1451         99          0         21        399
-/+ buffers/cache:       1030        520
Swap:         1999        642       1357
[root@pacteralinux /]# kill  2930   25599     63737
[root@pacteralinux /]# free -m
             total       used       free     shared    buffers     cached
Mem:          1551        594        957          0         22        396
-/+ buffers/cache:        175       1375
Swap:         1999        445       1554
[root@pacteralinux /]# swapoff /mnt/resource/sdc3mnt/swapfile
[root@pacteralinux /]# free -m
             total       used       free     shared    buffers     cached
Mem:          1551       1030        520          0         22        396
-/+ buffers/cache:        612        939
Swap:            0          0          0
[root@pacteralinux /]#

其实我想关闭虚拟内存是想把虚拟内存文件所在的分区挂在到另外的分区中去的,下面是其他的步骤:

  1. 关闭分区后才能卸载原来分区


  2. #关闭虚拟内存以及卸载分区
    [root@pacteralinux sdc3mnt]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1              29G   20G  7.5G  73% /
    tmpfs                 776M     0  776M   0% /dev/shm
    /dev/sdb1              69G  4.0G   62G   6% /mnt/resource
    /dev/sdc2             131G  2.2G  122G   2% /mnt/resource/sdc3mnt
    /dev/sdc1             362G   53G  291G  16% /usr/local/Tomcat_Trans/webapps/infoSocketService/logs
    [root@pacteralinux /]# swapoff /mnt/resource/sdc3mnt/swapfile
    [root@pacteralinux /]# umount /mnt/resource/sdc3mnt/


  3. #挂载分区
    [root@pacteralinux mnt]# mount /dev/sdc2 /mnt/mntsdc2formysql/
    [root@pacteralinux mnt]# df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1              29G   20G  7.5G  73% /
    tmpfs                 776M     0  776M   0% /dev/shm
    /dev/sdb1              69G  4.0G   62G   6% /mnt/resource
    /dev/sdc1             362G   53G  291G  16% /usr/local/Tomcat_Trans/webapps/infoSocketService/logs
    /dev/sdc2             131G  2.2G  122G   2% /mnt/mntsdc2formysql
    [root@pacteralinux mnt]# cd /mnt/mntsdc2formysql/
    [root@pacteralinux mntsdc2formysql]# ll
    total 2050020
    drwx------. 2 root root      16384 Dec  2 14:20 lost+found
    -rw-r--r--. 1 root root 2097152000 Dec  7 23:20 swapfile
    #重新开启虚拟分区
    [root@pacteralinux mntsdc2formysql]# swapon /mnt/mntsdc2formysql/swapfile
    [root@pacteralinux mntsdc2formysql]# free -m
                 total       used       free     shared    buffers     cached
    Mem:          1551       1036        515          0         24        398
    -/+ buffers/cache:        613        938
    Swap:         1999          0       1999
    [root@pacteralinux mntsdc2formysql]#


最后别忘了重启之前关闭的程序



----转

1、检查当前的分区情况:

   [root@localhost]#free -m

2、增加交换分区文件及大小,如果要增加2G大小的交换分区,则命令写法如下,其中的 count 等于想要的块大小。

   [root@localhost]# dd if=/dev/zero of=/home/swap bs=1024 count=2048000

3、设置交换文件:

   [root@localhost]# mkswap /home/swap

4、立即启用交换分区文件

    [root@localhost]# swapon /home/swap

5、如果要在引导时自动启用,则编辑 /etc/fstab 文件,添加行:
   /home/swap swap swap defaults 0 0

系统下次引导时,它就会启用新建的交换文件,再查看SWAP分区大小发现增加了2G。