今天遇到服务器无法SSH,VNC操作命令提示fork:cannot allocate memory

free查看内存还有(注意,命令可能要多敲几次才会出来)

查看最大进程数 sysctl kernel.pid_max

ps -eLf | wc -l查看进程数
 

确认是进程数满了

修改最大进程数后系统恢复
echo 1000000 > /proc/sys/kernel/pid_max

永久生效
echo "kernel.pid_max=1000000 " >> /etc/sysctl.conf
sysctl -p

(二)

[root@elcid-prod1 ~]# java -version

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00007ff55c5ea000, 4096, 0) failed; error='Cannot allocate memory' (errno=12)

#

# There is insufficient memory for the Java Runtime Environment to continue.

# Native memory allocation (mmap) failed to map 4096 bytes for committing reserved memory.

# An error report file with more information is saved as:

# /root/hs_err_pid5138.log

查找了一下相关文档,发现这个错误的含义其实就是像它自己说的,没法分配内存了。

The problem is inherent with the way Java allocates memory when executing processes. When Java executes a process, it must fork() then exec(). Forking creates a child process by duplicating the current process. By duplicating the current process, the new child process will request approximately the same amount of memory as its parent process, essentially doubling the memory required. However, this does not mean all the memory allocated will be used, as exec() is immediately called to execute the different code within the child process, freeing up this memory. As an example, when Stash tries to locate git, the Stash JVM process must be forked, approximately doubling the memory required by Stash.

解决方案呢有两个,第一个是用别的方法(例如 posix_spawn)替换掉 Java 的 fork/exec 方法从而申请到内存,第二个是开启系统的 Over-commit,跳过系统的可用内存检查直接分配。

第一个方法比较麻烦,需要使用一个 custom JNI binding 去更改 Java 底层调用,所以先不尝试。

第二个方法比较简单,但可能会导致依赖 C 语言 malloc() 的程序出错。

总之先试试第二个吧,毕竟就是改下系统变量的事儿。

临时更改: echo 1 > /proc/sys/vm/overcommit_memory

永久更改: 编辑 /etc/sysctl.conf,修改参数 vm.overcommit_memory = 1,重启服务器或者用户重新登录

(三)swappiness设置

swappiness的值的大小对如何使用swap分区是有着很大的联系的。swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。linux的基本默认设置为60,具体如下:

一般默认值都是60

[root@timeserver ~]# cat /proc/sys/vm/swappiness
60

也就是说,你的内存在使用到100-60=40%的时候,就开始出现有交换分区的使用。大家知道,内存的速度会比磁盘快很多,这样子会加大系统io,同时造的成大量页的换进换出,严重影响系统的性能,所以我们在操作系统层面,要尽可能使用内存,对该参数进行调整。

临时调整的方法如下,我们调成10:
[root@timeserver ~]# sysctl vm.swappiness=10
vm.swappiness = 10
[root@timeserver ~]# cat /proc/sys/vm/swappiness
10
这只是临时调整的方法,重启后会回到默认设置的

要想永久调整的话,需要将
需要在/etc/sysctl.conf修改,加上:
[root@timeserver ~]# cat /etc/sysctl.conf

# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
vm.swappiness=10

激活设置

[root@timeserver ~]# sysctl -p

在linux中,可以通过修改swappiness内核参数,降低系统对swap的使用,从而提高系统的性能。

遇到的问题是这样的,新版本产品发布后,每小时对内存的使用会有一个尖峰。虽然这个峰值还远没有到达服务器的物理内存,但确发现内存使用达到峰值时系统开始使用swap。在swap的过程中系统性能会有所下降,表现为较大的服务延迟。对这种情况,可以通过调节swappiness内核参数降低系统对swap的使用,从而避免不必要的swap对性能造成的影响。

简单地说这个参数定义了系统对swap的使用倾向,默认值为60,值越大表示越倾向于使用swap。可以设为0,这样做并不会禁止对swap的使用,只是最大限度地降低了使用swap的可能性。

通过sysctl -q vm.swappiness可以查看参数的当前设置。

修改参数的方法是修改/etc/sysctl.conf文件,加入vm.swappiness=xxx,并重起系统。这个操作相当于是修改虚拟系统中的/proc/sys/vm/swappiness文件,将值改为XXX数值。

如果不想重起,可以通过sysctl -p动态加载/etc/sysctl.conf文件,但建议这样做之前先清空swap。

-bash: fork: Cannot allocate memory的更多相关文章

  1. -bash: fork: Cannot allocate memory 问题的处理

    今天生产机器突然无法登录了,正好有一个用top挂着,但是退出top,执行任何命令都报-bash: fork: Cannot allocate memory,但是查看内存还是有很多空闲,然后在百度上查了 ...

  2. 问题:Linux 输入任何命令都显示 -bash: fork: Cannot allocate memory

    应该是某个程序吃掉了所有的内存,只能重启

  3. df卡死和fork:cannot allocate memory报错

    早上到了公司,发现docker资源池的某一台主机根文件系统写满. 检查后发现该主机/data目录未挂载文件系统,直接放在了根目录下.于是联系业务方将应用迁移,联系主机工程师为/data挂载80G的存储 ...

  4. redis 写磁盘出错 Can’t save in background: fork: Cannot allocate memory (转)

    查看 Redis 日志 发现系统在频繁报错: [26641] 18 Dec 04:02:14 * 1 changes in 900 seconds. Saving… [26641] 18 Dec 04 ...

  5. terminal Failed to fork(connot allocate memory)问题处理

    今天遇到服务器无法SSH,VNC操作命令提示fork:cannot allocate memory free查看内存还有(注意,命令可能要多敲几次才会出来) 查看最大进程数 sysctl kernel ...

  6. redis : Can't save in background: fork: Cannot allocate memory

    redis : Can't save in background: fork: Cannot allocate memory JAVA程序报错信息: MISCONF Redis is configur ...

  7. Redis报错Can't save in background: fork: Cannot allocate memory及类似问题的处理方法

    问题的发现及解决过程: 1.Redis主从复制(一主一从)环境在客户端用命令查看主从状态 在slave上输入命令显示如下: 在master上输入命令显示如下: 从显示可以看出主从关系出现问题,然后查看 ...

  8. fork failed.: Cannot allocate memory

    在做压力测试时候: [root@666 ok]# webbench -c 5000 -t30 http://10.100.0.61/ Webbench - Simple Web Benchmark 1 ...

  9. linux下的缓存机制buffer、cache、swap - 运维总结 ["Cannot allocate memory"问题]

    一.缓存机制介绍 在Linux系统中,为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果 ...

随机推荐

  1. 【Android】Android布局文件的一些属性值

    第一类:属性值 true或者 false   android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 androi ...

  2. Android ShareUserId 使用总结

    今天讲一下Android里面经常看到却不太留意的知识点——ShareUserId,在Android里面每个app都有一个唯一的linux user ID,则这样权限就被设置成该应用程序的文件只对该用户 ...

  3. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

    文章转载自:https://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PH ...

  4. swift 遍历枚举

      // see at http://swifter.tips/enum-enumerate/ // 貌似有些空格在粘贴的时候没有了...    = =! import Foundation   en ...

  5. Java知多少(9) import及Java类的搜索路径

    如果你希望使用Java包中的类,就必须先使用import语句导入. import语句与C语言中的 #include 有些类似,语法为:    import package1[.package2…].c ...

  6. hadoop 在centos中的搭建

    总体思路,准备主从服务器,配置主服务器可以无密码SSH登录从服务器,解压安装JDK,解压安装Hadoop,配置hdfs.mapreduce等主从关系. 1.环境,3台CentOS7,64位,Hadoo ...

  7. java二叉树字典查询(qq 928900200)

    This assignment will help you practice and understand better the Binary Tree and Binary Search Tree ...

  8. 【转】svn:is not under version control and is not part of the commit, yet its child解决办法

    来自:http://blog.csdn.net/lufeng20/article/details/7641093 在把写好的代码提交到svn上面时,遇到了一个错误如下: svn: Commit fai ...

  9. windows下添加多个git仓库账号

    当使用git方式下载时,如果没有配置过ssh key,会提示错误(git clone支持https和git(即ssh)两种方式下载源码) 当需要在机器上使用不同的git账户,这就需要知道如何在机器上添 ...

  10. [JS] ECMAScript 6 - Array : compare with c#

    扩展运算符(spread) 先复习下 rest 参数. (1) argument模式,但不够好. // https://blog.csdn.net/weixin_39723544/article/de ...