java环境内存不够导致swap

云服务器默认不支持创建swap,所以部分场景下需要 创建一个文件让文件成为swap。

#1.创建一个文件块

[root@oldboyedu01-nb ~]# dd if=/dev/zero of=/tmp/100m bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.935927 s, 112 MB/s
[root@oldboyedu01-nb ~]# file /tmp/100m 
/tmp/100m: data

#2.变成swap

[root@oldboyedu01-nb ~]# mkswap  /tmp/100m 
mkswap: /tmp/100m: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=1b2aea27-a092-4345-806f-b95b8d33873a
[root@oldboyedu01-nb ~]# file /tmp/100m 
/tmp/100m: Linux/i386 swap file (new style) 1 (4K pages) size 25599 pages

#3.swap生效

[root@oldboyedu01-nb ~]# swapon /tmp/100m 
[root@oldboyedu01-nb ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          3.9G       398M       3.5G       248K        65M       134M
-/+ buffers/cache:       198M       3.7G
Swap:         867M         0B       867M
[root@oldboyedu01-nb ~]# swapon -s
Filename				Type		Size	Used	Priority
/dev/sda2               partition	786428	0	    -1
/tmp/100m               file		102396	0	    -2

#4.swap永久生效

###方法1. /etc/rc.local 
swapon /tmp/100m 

###方法2. /etc/fstab 
/tmp/100m               swap                    swap    defaults        0 0
[root@oldboyedu01-nb ~]# free -h
total       used       free     shared    buffers     cached
Mem:          3.9G       398M       3.5G       248K        65M       134M
-/+ buffers/cache:       199M       3.7G
Swap:         867M         0B       867M

Linux的特点:会把使用过的命令和数据临时存放在内存中 cache和buffer,所以上面真实可用空间为3.7G。