<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ulimit &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/ulimit/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 18:04:15 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 使用ThreadPoolExecutor产生的 OutOfMemoryError: unable to create new native thread 错误</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8threadpoolexecutor%e4%ba%a7%e7%94%9f%e7%9a%84-outofmemoryerror-unable-to-create-new-native-thread-%e9%94%99%e8%af%af/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8threadpoolexecutor%e4%ba%a7%e7%94%9f%e7%9a%84-outofmemoryerror-unable-to-create-new-native-thread-%e9%94%99%e8%af%af/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 04 Sep 2015 06:11:11 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[max user processes]]></category>
		<category><![CDATA[OutOfMemoryError]]></category>
		<category><![CDATA[ThreadPoolExecutor]]></category>
		<category><![CDATA[ulimit]]></category>
		<category><![CDATA[unable to create new native thread]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8530</guid>

					<description><![CDATA[<p>
最近，在使用Java的ThreadPoolExecutor来实现一个并发功能的时候，发现程序刚执行起来不久，就提示了错误：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
OutOfMemoryError: unable to create new native thread
</pre>
<p>并且服务器立即陷入类似于&#8220;无响应&#8221;的状态，无法用Ctrl+C结束掉我的Java程序，按Ctrl+C的时候，命令行只是不断地打印出类似于下面的消息：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated
</pre>
<p><span id="more-8530"></span><br />
在其他已经连上的terminal下，我想尝试用 ps -ef &#124; grep xxx 来找出进程的pid并kill掉它，也无果，因为只要输完ps命令，再一回车，马上就提示错误（大概意思就是资源不足），于是在不重启系统的情况下，只能静静等待，直到Linux系统恢复了响应，就可以用ps -ef查出其pid并kill掉它了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
但是为嘛会出现这样的问题？<br />
首先，服务器内存是非常充足的，不应该是内存真的不够用；其次，我仔细检查了一遍我的代码，也并无异常之处；再次，我的程序也不会占用非常大的内存。<br />
于是我问了一下Google，找到了原因所在：我程序中设置的ThreadPoolExecutor并发数超出了系统限制。这个限制，就是所谓的&#8220;<span style="color:#0000ff;">max user processes</span>&#8221;限制，可以用如下命令查看Linux系统的设置值：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ulimit -a
</pre>
<p>其输出类似于：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 385903
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
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) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
</pre>
<p>看到&#8220;<span style="color:#0000ff;">max user processes</span>&#8221;那一行了吗？1024就是我当前用户的最大允许值，当超过这个值的时候，就无法创建新的process了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8threadpoolexecutor%e4%ba%a7%e7%94%9f%e7%9a%84-outofmemoryerror-unable-to-create-new-native-thread-%e9%94%99%e8%af%af/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
最近，在使用Java的ThreadPoolExecutor来实现一个并发功能的时候，发现程序刚执行起来不久，就提示了错误：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
OutOfMemoryError: unable to create new native thread
</pre>
<p>并且服务器立即陷入类似于&ldquo;无响应&rdquo;的状态，无法用Ctrl+C结束掉我的Java程序，按Ctrl+C的时候，命令行只是不断地打印出类似于下面的消息：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated
</pre>
<p><span id="more-8530"></span><br />
在其他已经连上的terminal下，我想尝试用 ps -ef | grep xxx 来找出进程的pid并kill掉它，也无果，因为只要输完ps命令，再一回车，马上就提示错误（大概意思就是资源不足），于是在不重启系统的情况下，只能静静等待，直到Linux系统恢复了响应，就可以用ps -ef查出其pid并kill掉它了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
但是为嘛会出现这样的问题？<br />
首先，服务器内存是非常充足的，不应该是内存真的不够用；其次，我仔细检查了一遍我的代码，也并无异常之处；再次，我的程序也不会占用非常大的内存。<br />
于是我问了一下Google，找到了原因所在：我程序中设置的ThreadPoolExecutor并发数超出了系统限制。这个限制，就是所谓的&ldquo;<span style="color:#0000ff;">max user processes</span>&rdquo;限制，可以用如下命令查看Linux系统的设置值：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
ulimit -a
</pre>
<p>其输出类似于：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 385903
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65535
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) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
</pre>
<p>看到&ldquo;<span style="color:#0000ff;">max user processes</span>&rdquo;那一行了吗？1024就是我当前用户的最大允许值，当超过这个值的时候，就无法创建新的process了。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
对应到使用ThreadPoolExecutor的代码，当我们创建一个对象的时候，会使用如下的构造函数：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
                              long keepAliveTime, TimeUnit unit,
                              BlockingQueue&lt;Runnable&gt; workQueue) {
    //...
}
</pre>
<p>其中，第1和第2个参数，就是线程池里线程数的大小：</p>
<blockquote>
<div>
		@param corePoolSize the number of threads to keep in the pool, even if they are idle.</div>
<div>
		@param maximumPoolSize the maximum number of threads to allow in the pool.</div>
</blockquote>
<p>虽然我只把这两个数值分别设置成了400和500，但是由于我的用户下还运行有其他很多程序，因此，它们加在一起占用的线程数超过了1024的时候，就会出现前面的错误。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
要修改Linux系统中的这个限制，可以修改&nbsp;<span style="color:#0000ff;">/etc/security/limits.d/90-nproc.conf</span> 文件，其内容类似于：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
# Default limit for number of user&#39;s processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

*          soft    nproc     1024
root       soft    nproc     unlimited
</pre>
<p>把里面的数值改掉即可。</p>
<p>由于不方便修改Linux系统配置，因此，我把ThreadPoolExecutor的线程数调小到100，发现程序运行起来再也没有出现过上面的问题，非常稳定。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8threadpoolexecutor%e4%ba%a7%e7%94%9f%e7%9a%84-outofmemoryerror-unable-to-create-new-native-thread-%e9%94%99%e8%af%af/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
