[Linux]调整swap

在启动Tomcat的过程中,tomcat/catalina.out中报出如下故障:

> /opt/govern/wydaas/logs/catalina.out
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 715653120 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /root/hs_err_pid19148.log

> /root/hs_err_pid19148.log
# There is insufficient memory for the Java Runtime Environment to continue. 
# Native memory allocation (mmap) failed to map 715653120 bytes for committing reserved memory.


1 JRE/JVM 的内存需求大小:715653120Byte/1024/1024=682.5MB ,但内存空间提供不了这么大,导致应用服务启动失败。
2 说明了JRE(JVM)内存不足,但可以通过增大swap的存储空间来解决。

1 环境声明

CentOS7 / x86 / 64bit

2 查看 swap存储空间

[root@hydrant ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:          11903       11358         168          71         376         176
Swap:         16383        8569        7814
[root@hydrant ~]# swapon -s 
文件名                类型        大小    已用    权限
/tmp/mem.swap                              file    4194300    0    -1
/dev/dm-1                              	partition	8388604	8151604	-2
[root@hydrant ~]# cat /proc/swaps
文件名                类型        大小    已用    权限
/tmp/mem.swap                              file    4194300    0    -1
/dev/dm-1                              	partition	8388604	8151604	-2

3 增大 swap存储空间

方式1:文件法

通过新增swap可临时存储的交换文件来增大swap存储空间

  • step1 新创建1个总存储空间为8GB的交换文件(/swap01)
[root@hydrant ~]# dd if=/dev/zero of=/swap01 bs=1M count=8192
记录了8192+0 的读入
记录了8192+0 的写出
8589934592字节(8.6 GB)已复制,87.0453 秒,98.7 MB/秒
  • step2 设置交换文件

把这个文件变成swap文件

[root@hydrant ~]# mkswap /swap01
正在设置交换空间版本 1,大小 = 8388604 KiB
无标签,UUID=36f7531f-eecc-4133-9b14-0ab163c4cb40
  • step3 立即启用交换分区文件

启用这个swap文件

[root@hydrant ~]# swapon /swap01
swapon: /swap01:不安全的权限 0644,建议使用 0600。
  • step4 编辑/etc/fstab文件,使在每次开机时自动加载swap文件
/swap01    swap    swap    default   0 0

方式2:磁盘分区法

通过新增swap磁盘分区来增大swap

  • step1 通过fdisk新增分区
fdisk  /dev/sda
Command (m for help): n

通过n新建分区,选择为主分区,分区ID为82,即swap分区。保存退出。

  • step2 对新建的分区进行【格式化】,并【挂载】
mkswap /dev/sda4
swapon /dev/swap
  • step3 完成后,参照方法一里的查看方法进行查看确认。新增完成后,在/etc/fstab里新增如下配置
/dev/sda4    swap    swap    default   0 0

4 卸载指定的swap区

swapoff /tmp/mem.swap

5 参考文献

posted @ 2020-06-24 09:04  千千寰宇  阅读(380)  评论(0编辑  收藏  举报