去掉LINUX系统对用户的内存大小和文件句柄限制等限制

本文介绍了如何解除Linux系统对用户内存大小和文件句柄的限制,以增加可创建的进程数量。通过修改/etc/security/limits.conf、/etc/profile和/etc/pam.d/login文件,设置不限制的内存锁和最大进程数。方法包括设置unlimited的memlock限制,以及调整maxuser processes的值,确保在系统重启后生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

去掉内存和文件句柄限制

以coherence用户为例子:


修改:/etc/security/limits.conf

 

coherence hard nproc 655360

coherence soft nproc 655360

 

coherence hard nofile 65536

coherence soft nofile 65536

 

*           hard  memlock           unlimited
*            soft  memlock           unlimited

 

还要修改PROFILE

/etc/profile

ulimit -l unlimited

ulimit -u unlimited

 

还要修改

/etc/pam.d/login

session   required    /lib/security/pam_limits.so

 

需要重启才能生效。

 

执行:ulimit–l 确认

 

. Method 1:Changing Locked Memory Permanently

https://software.intel.com/en-us/blogs/2014/12/16/best-known-methods-for-setting-locked-memory-size

 

This method shows how to alter a configuration limit to allow auser to change the locked memory size.

1.1 Change locked memory size in the host:

Edit the file /etc/security/limits.conf

$ sudo vi /etc/security/limits.conf

Add the following line at the end of the file to set anunlimited locked memory for the user named “user1”. The second field value canbe “hard” or “soft”: “hard” enforces the resource limit, and “soft” defines thedefault value within the maximum value specified by “hard”.

user1    hard  memlock           unlimited
user1    soft   memlock           unlimited

By adding this, user1 can raise the locked memory size withoutlimit. To allow all users to set unlimited memlock, simply replace user1 withthe wildcard *

*           hard  memlock           unlimited
*            soft  memlock           unlimited

Reboot the system. Login and verify that the locked memory isnow set to unlimited:

$ ulimit –l
unlimited

 

So you limits.conf would have the line (to a maximum of 4G of memory)

luser  hard  as  4000000

 

单位是K

 

 

To increase the soft and hard limits ofmemlock, the following lines should be added to the file /etc/security/limits.conf:

username or * hard memlock size_in_kbytes

username or * soft memlock size_in_kbytes

For example, to set unlimited space forall users:

* soft memlockunlimited

* hard memlockunlimited

To set the limits for all users to 256MB:

* soft memlock262144

* hard memlock262144

 

 

Java.lang.OutOfMemoryError: unable to create new nativethread

最近在Linux服务器上发布应用时碰到一个如下的异常:

Caused by: java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:640)

初看可能会认为是系统的内存不足,如果这样想的话就被这段提示带到沟里面去了。
上面这段错误提示的本质是Linux操作系统无法创建更多进程,导致出错。因此要解决这个问题需要修改Linux允许创建更多的进程。

修改Linux最大进程数

我们可以通过ulimit-a来查看当前Linux系统的一些系统参数。

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 62357
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

在上面这些参数中,通常我们关注得比较多的是一个进程可打开的最大文件数,即openfiles。系统允许创建的最大进程数量即是maxuser processes 这个参数。我们可以使用ulimit -u 4096 修改maxuser processes的值,但是只能在当前终端的这个session里面生效,重新登录后仍然是使用系统默认值。
正确的修改方式是修改/etc/security/limits.d/90-nproc.conf文件中的值。先看一下这个文件包含什么:

$ cat /etc/security/limits.d/90-nproc.conf 
# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.
 
*          soft    nproc    4096

我们只要修改上面文件中的4096这个值,即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值