中国英雄辈出顺利 發表於 2019-11-22 14:21:00

【Linux】解决Linux服务器内存不足问题

<p>在Linux服务器上使用Jmeter进行压测的时候,遇到了一个错误:</p>
<p><img src="https://img2018.cnblogs.com/blog/351541/201911/351541-20191122141601426-1920486178.png" alt=""></p>
<p>&nbsp;</p>
<p>根据这里的提示:</p>
<div class="cnblogs_code">
<pre>Java HotSpot(TM) <span style="color: rgba(128, 0, 128, 1)">64</span>-Bit Server VM warning: INFO: os::commit_memory(<span style="color: rgba(128, 0, 128, 1)">0x00000000c0000000</span>, <span style="color: rgba(128, 0, 128, 1)">1073741824</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>) failed; error=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Cannot allocate memory</span><span style="color: rgba(128, 0, 0, 1)">'</span> (errno=<span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">)
#
# There is insufficient memory </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map </span><span style="color: rgba(128, 0, 128, 1)">1073741824</span> bytes <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> committing reserved memory.
# An error report </span><span style="color: rgba(0, 0, 255, 1)">file</span> with <span style="color: rgba(0, 0, 255, 1)">more</span><span style="color: rgba(0, 0, 0, 1)"> information is saved as:
# </span>/home/HAO/IM/hs_err_pid20194.log</pre>
</div>
<p>去查了一下内存情况,可以看到结果如下:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">free</span> -<span style="color: rgba(0, 0, 0, 1)">m
             total       used       </span><span style="color: rgba(0, 0, 255, 1)">free</span><span style="color: rgba(0, 0, 0, 1)">   shared    buffers   cached
Mem:          </span><span style="color: rgba(128, 0, 128, 1)">8001</span>       <span style="color: rgba(128, 0, 128, 1)">7881</span>      <span style="color: rgba(128, 0, 128, 1)">120</span>          <span style="color: rgba(128, 0, 128, 1)">0</span>          <span style="color: rgba(128, 0, 128, 1)">4</span>      <span style="color: rgba(128, 0, 128, 1)">416</span>
-/+ buffers/cache:       <span style="color: rgba(128, 0, 128, 1)">7460</span>      <span style="color: rgba(128, 0, 128, 1)">541</span><span style="color: rgba(0, 0, 0, 1)">
Swap:            </span><span style="color: rgba(128, 0, 128, 1)">0</span>          <span style="color: rgba(128, 0, 128, 1)">0</span>          <span style="color: rgba(128, 0, 128, 1)">0</span></pre>
</div>
<p>确实是内存不足了,去查一下,里面有很多不认识的服务。于是准备清理一下。</p>
<p>写了个脚本运行,以后可以复用:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">kill</span> -<span style="color: rgba(128, 0, 128, 1)">9</span> `<span style="color: rgba(0, 0, 255, 1)">ps</span> aux|<span style="color: rgba(0, 0, 255, 1)">head</span> -<span style="color: rgba(128, 0, 128, 1)">1</span>;<span style="color: rgba(0, 0, 255, 1)">ps</span> aux|<span style="color: rgba(0, 0, 255, 1)">grep</span> -v PID|<span style="color: rgba(0, 0, 255, 1)">sort</span> -rn -k +<span style="color: rgba(128, 0, 128, 1)">4</span>|<span style="color: rgba(0, 0, 255, 1)">head</span> | <span style="color: rgba(0, 0, 255, 1)">awk</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">{print $2}</span><span style="color: rgba(128, 0, 0, 1)">'</span>`</pre>
</div>
<p>如果执行之后在查询,发现内存还是不够,可以用下面的多执行几次:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">kill</span> -<span style="color: rgba(128, 0, 128, 1)">9</span> `<span style="color: rgba(0, 0, 255, 1)">ps</span> aux|<span style="color: rgba(0, 0, 255, 1)">head</span> -<span style="color: rgba(128, 0, 128, 1)">1</span>;<span style="color: rgba(0, 0, 255, 1)">ps</span> aux|<span style="color: rgba(0, 0, 255, 1)">grep</span> -v PID|<span style="color: rgba(0, 0, 255, 1)">sort</span> -rn -k +<span style="color: rgba(128, 0, 128, 1)">4</span>|<span style="color: rgba(0, 0, 255, 1)">head</span> -K <span style="color: rgba(128, 0, 128, 1)">50</span>| <span style="color: rgba(0, 0, 255, 1)">awk</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">{print $2}</span><span style="color: rgba(128, 0, 0, 1)">'</span>`</pre>
</div>
<p>再次查询,可见已经有不少可用内存了:</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">free</span> -<span style="color: rgba(0, 0, 0, 1)">m
             total       used       </span><span style="color: rgba(0, 0, 255, 1)">free</span><span style="color: rgba(0, 0, 0, 1)">   shared    buffers   cached
Mem:          </span><span style="color: rgba(128, 0, 128, 1)">8001</span>       <span style="color: rgba(128, 0, 128, 1)">2460</span>       <span style="color: rgba(128, 0, 128, 1)">5541</span>          <span style="color: rgba(128, 0, 128, 1)">0</span>          <span style="color: rgba(128, 0, 128, 1)">5</span>      <span style="color: rgba(128, 0, 128, 1)">663</span>
-/+ buffers/cache:       <span style="color: rgba(128, 0, 128, 1)">1791</span>       <span style="color: rgba(128, 0, 128, 1)">6209</span><span style="color: rgba(0, 0, 0, 1)">
Swap:            </span><span style="color: rgba(128, 0, 128, 1)">0</span>          <span style="color: rgba(128, 0, 128, 1)">0</span>          <span style="color: rgba(128, 0, 128, 1)">0</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>下面补充一下更多的命令;</p>
<p>linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合:<br><br>ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head<br><br><br><br>linux下获取占用内存资源最多的10个进程,可以使用如下命令组合:<br><br>ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<br><br><br><br><br>命令组合解析(针对CPU的,MEN也同样道理):<br><br>ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head<br><br><br><br>该命令组合实际上是下面两句命令:<br><br>ps aux|head -1<br><br>ps aux|grep -v PID|sort -rn -k +3|head</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>可以使用一下命令查使用内存最多的10个进程</p>
<p><strong>查看占用cpu最高的进程</strong></p>
<p>ps&nbsp;aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head</p>
<p>或者top (然后按下M,注意这里是大写)</p>
<p><strong>查看占用内存最高的进程</strong></p>
<p>ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head</p>
<p>或者top (然后按下P,注意这里是大写)</p>
<p><strong>该命令组合实际上是下面两句命令:</strong></p>
<p>ps aux|head -1<br>ps aux|grep -v PID|sort -rn -k +3|head</p>
<p>其中第一句主要是为了获取标题(USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND)。<br>接下来的grep -v PID是将ps aux命令得到的标题去掉,即grep不包含PID这三个字母组合的行,再将其中结果使用sort排序。<br>sort -rn -k +3该命令中的-rn的r表示是结果倒序排列,n为以数值大小排序,而-k +3则是针对第3列的内容进行排序,再使用head命令获取默认前10行数据。(其中的|表示管道操作)</p>
<p><strong>补充:内容解释</strong></p>
<p>PID:进程的ID<br>USER:进程所有者<br>PR:进程的优先级别,越小越优先被执行<br>NInice:值<br>VIRT:进程占用的虚拟内存<br>RES:进程占用的物理内存<br>SHR:进程使用的共享内存<br>S:进程的状态。S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值为负数<br>%CPU:进程占用CPU的使用率<br>%MEM:进程使用的物理内存和总内存的百分比<br>TIME+:该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值。<br>COMMAND:进程启动命令名称</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;</p>
<p>可以使用以下命令查使用内存最多的K个进程</p>
<p>方法1:</p>
<div class="cnblogs_code">
<pre>ps -aux | sort -k4nr | head -K</pre>
</div>
<p>如果是10个进程,K=10,如果是最高的三个,K=3</p>
<p><strong>说明:</strong>ps -aux中(a指代all——所有的进程,u指代userid——执行该进程的用户id,x指代显示所有程序,不以终端机来区分)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps -aux的输出格式如下:</p>
<div class="cnblogs_code">
<pre>USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         10.00.0193521308 ?      Ss   Jul29   0:00 /sbin/init
root         20.00.0      0   0 ?      S    Jul29   0:00
root         30.00.0      0   0 ?      S    Jul29   0:11 </pre>
</div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort -k4nr中(k代表从第几个位置开始,后面的数字4即是其开始位置,结束位置如果没有,则默认到最后;n指代numberic sort,根据其数值排序;r指代reverse,这里是指反向比较结果,输出时默认从小到大,反向后从大到小。)。本例中,可以看到%MEM在第4个位置,根据%MEM的数值进行由大到小的排序。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head -K(K指代行数,即输出前几位的结果)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|为管道符号,将查询出的结果导到下面的命令中进行下一步的操作。</p>
<p>方法2:top&nbsp;(然后按下M,注意大写)</p>
<p>二、可以使用下面命令查使用CPU最多的K个进程</p>
<p>方法1:</p>
<div class="cnblogs_code">
<pre>ps -aux | sort -k3nr | head -K</pre>
</div>
<p>方法2:top&nbsp;(然后按下P,注意大写</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>待续</p>

</div>
<div id="MySignature" role="contentinfo">
    <div class="div_masklayer" id="div_masklayer"></div>
<div class="div_popup" id="Div_popup"> <img class="img_zfb" id="img_zfb" src="http://files.cnblogs.com/files/haochuang/alipay-RedEnvelopes.bmp">
<p class="mid"> 赠人玫瑰 <br> 手留余香 </p>

</div>


<div class="autograph">
<p>我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容……我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系!-杨绛先生</p>

<p> <span style="display: none">如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【<strong>推荐</strong>】按钮。<br>

    </span>如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【<strong>关注我</strong>】。</p>


</div><br><br>
来源:https://www.cnblogs.com/haochuang/p/11911305.html
頁: [1]
查看完整版本: 【Linux】解决Linux服务器内存不足问题