win10正常内存占用用不正常 异常的高

继上一次查应用的CPU飙高问题(/hzmark/p/JVM_CPU.html)过去10天了。上次只是定位到了是一个第三方包占用了大量的CPU使用,但没有细致的去查第三方包为什么占用了这么高的CPU,并且内存为什么如此诡异。总的来说上一次排查带来的收获是熟悉了JVM的工具使用和大致定位到了问题。
在上次排查问题之后,应用出现异常的频率还是较高,终下定决心再查一次,而这次排查的重点落在内存方面。因为怀疑CPU偏高是因为内存的异常导致频繁的GC引起的。
首先是JVM内存的状态:
S0和S1交替的用满,不停的在进行YGC。
使用jmap可以看到内存中那些对象使用了大量的内存:
下面是列出内存占用的方法(下图不是异常状态的时候的现象):
在异常状态的情况下看到的现象是class name:[B占用好几个G的内存,也就是byte数组占用了很大的内存。
结合上一次查CPU问题时定位到的问题代码:
仔细看出问题的线程:com.trilead.ssh2.StreamGobbler$GobblerThread.run
到这里为止都是通过上次排查CPU问题可以推测出来的,其实仔细一点耐心一点顺着com.trilead.ssh2.StreamGobbler$GobblerThread.run继续往下分析能定位到具体问题,但是到这里之后我才用了另一种方式去定位问题,那就是分析出现问题时的堆内存使用情况,下面是我的分析过程。
首先dump内存数据:
jmap -dump:format=b,file=xxx.bin 3230
其中xxx.bin是dump出来的文件,3230是pid
之后使用mat打开该文件(这边不介绍MAT的使用,可以自己查找帮助文档;我使用的是eclipse的mat插件,可以在官网上找到update的地址在线安装就可以了http://download.eclipse.org/mat/1.3.1/update-site/)
这是一个概览信息,可以看到总共使用了618M内存,而有一块直接占用了576M,那么问题肯定出现在这块内存中。
点到leak suspects会有一个系统分析的怀疑问题:
和上一步我们的猜想是一致的,问题出在最大的这个内存这里。和排查CPU时得出的结论也是一致的,问题出在第三方的包内。
点击detail内容可以看到具体是哪里占用了内存。
通过with outgoing references查看线程引用的对象,如下图所示:
在找到使用内存最大的对象,查找他的引用,可以看到代码中是哪里引用了这块内存导致无法被回收
到这里位置就可以知道是自己起的线程保持了对内存的引用导致无法别回收。查自己写的代码已经可以精确的定位到问题。(其实根据上次使用到一些JVM的工具已经可以发现问题,但是问了跟精确,顺便学习内存排查,所以有了MAT分析的过程)
下面是定位到问题代码之后的处理。
查看具体的代码:
1 package com.trilead.ssh2;
3 import java.io.IOE
4 import java.io.InputS
5 import java.io.InterruptedIOE
* A &code&StreamGobbler&/code& is an InputStream that uses an internal worker
* thread to constantly consume input from another InputStream. It uses a buffer
* to store the consumed data. The buffer size is automatically adjusted, if needed.
* This class is sometimes very convenient - if you wrap a session's STDOUT and STDERR
* InputStreams with instances of this class, then you don't have to bother about
* the shared window of STDOUT and STDERR in the low level SSH-2 protocol,
* since all arriving data will be immediatelly consumed by the worker threads.
* Also, as a side effect, the streams will be buffered (e.g., single byte
* read() operations are faster).
* Other SSH for Java libraries include this functionality by default in
* their STDOUT and STDERR InputStream implementations, however, please be aware
* that this approach has also a downside:
* If you do not call the StreamGobbler's &code&read()&/code& method often enough
* and the peer is constantly sending huge amounts of data, then you will sooner or later
* encounter a low memory situation due to the aggregated data (well, it also depends on the Java heap size).
* Joe Average will like this class anyway - a paranoid programmer would never use such an approach.
* The term "StreamGobbler" was taken from an article called "When Runtime.exec() won't",
* see /javaworld/jw-12-2000/jw-1229-traps.html.
* @author Christian Plattner,
* @version $Id: StreamGobbler.java,v 1.1
12:49:56 cplattne Exp $
35 public class StreamGobbler extends InputStream
class GobblerThread extends Thread
public void run()
byte[] buff = new byte[8192];
while (true)
int avail = is.read(buff);
synchronized (synchronizer)
if (avail &= 0)
isEOF = true;
synchronizer.notifyAll();
int space_available = buffer.length - write_
if (space_available & avail)
/* compact/resize buffer */
int unread_size = write_pos - read_
int need_space = unread_size +
byte[] new_buffer =
if (need_space & buffer.length)
int inc = need_space / 3;
inc = (inc & 256) ? 256 :
inc = (inc & 8192) ? 8192 :
new_buffer = new byte[need_space + inc];
if (unread_size & 0)
System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size);
buffer = new_
read_pos = 0;
write_pos = unread_
System.arraycopy(buff, 0, buffer, write_pos, avail);
write_pos +=
synchronizer.notifyAll();
catch (IOException e)
synchronized (synchronizer)
exception =
synchronizer.notifyAll();
private InputS
private final GobblerT
private final Object synchronizer = new Object();
private boolean isEOF = false;
private boolean isClosed = false;
private IOException exception = null;
private byte[] buffer = new byte[2048];
private int read_pos = 0;
private int write_pos = 0;
public StreamGobbler(InputStream is)
t = new GobblerThread();
t.setDaemon(true);
t.start();
public int read() throws IOException
synchronized (synchronizer)
if (isClosed)
throw new IOException("This StreamGobbler is closed.");
while (read_pos == write_pos)
if (exception != null)
if (isEOF)
return -1;
synchronizer.wait();
catch (InterruptedException e)
throw new InterruptedIOException();
int b = buffer[read_pos++] & 0xff;
public int available() throws IOException
synchronized (synchronizer)
if (isClosed)
throw new IOException("This StreamGobbler is closed.");
return write_pos - read_
public int read(byte[] b) throws IOException
return read(b, 0, b.length);
public void close() throws IOException
synchronized (synchronizer)
if (isClosed)
isClosed = true;
isEOF = true;
synchronizer.notifyAll();
is.close();
public int read(byte[] b, int off, int len) throws IOException
if (b == null)
throw new NullPointerException();
if ((off & 0) || (len & 0) || ((off + len) & b.length) || ((off + len) & 0) || (off & b.length))
throw new IndexOutOfBoundsException();
if (len == 0)
synchronized (synchronizer)
if (isClosed)
throw new IOException("This StreamGobbler is closed.");
while (read_pos == write_pos)
if (exception != null)
if (isEOF)
return -1;
synchronizer.wait();
catch (InterruptedException e)
throw new InterruptedIOException();
int avail = write_pos - read_
avail = (avail & len) ? len :
System.arraycopy(buffer, read_pos, b, off, avail);
read_pos +=
如果使用这个类之前知悉读类的说明信息是可以看明白这个类存在的问题的。当然也可以从源码上看出来,我就是从源码看出问题的,当时花的时间肯定比看说明耗费的时间长。
下面是产生问题的代码:
1 class GobblerThread extends Thread
public void run()
byte[] buff = new byte[8192];
while (true)
int avail = is.read(buff);
synchronized (synchronizer)
if (avail &= 0)
isEOF = true;
synchronizer.notifyAll();
int space_available = buffer.length - write_
if (space_available & avail)
/* compact/resize buffer */
int unread_size = write_pos - read_
int need_space = unread_size +
byte[] new_buffer =
if (need_space & buffer.length)
int inc = need_space / 3;
inc = (inc & 256) ? 256 :
inc = (inc & 8192) ? 8192 :
new_buffer = new byte[need_space + inc];
if (unread_size & 0)
System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size);
buffer = new_
read_pos = 0;
write_pos = unread_
System.arraycopy(buff, 0, buffer, write_pos, avail);
write_pos +=
synchronizer.notifyAll();
catch (IOException e)
synchronized (synchronizer)
exception =
synchronizer.notifyAll();
StreamGobbler类的buffer属性是一个只会不断增长,且不会清空的byte数组,问题就出在这里。
当一个Hive查询的执行时间非常的长(比如出现数据倾斜导致查询进行不下去、执行任务很多需要排队导致时间边长),那么这个线程将持续很长时间,而它将hold这个buffer永远不会被回收,虽然它里面的内容再也不会被使用。
那么这就是不合理的地方。StreamGobbler类的设计就是启动一个线程将一个输入流的内容不停的清空,读到自己的缓存中。这样的目的是不要造成流的堵塞,但这样就不适用长任务且日志不停输出的场景。
至此排查分析结束,原因就是第三方的StreamGobbler并不适合当前的场景。解决的办法就是自己实现读取流的线程及时的处理掉流中的内容,和StreamGobbler起一个线程去读试一致的,只是不做缓存处理。
经过这次CPU飙高查到原因,基本已经熟悉了JVM工具的使用,CPU问题及内存问题的排查流程,算是有些收获。看问题,排查定位问题,看源码分析,最终解决掉问题的过程让自己非常的&爽&,并且得到成长。
和各位博友一起成长,加油。
阅读(...) 评论()windows服务主进程,物理内存占用瞬间飙至异常,到底是什么原因? - 知乎46被浏览19264分享邀请回答239 条评论分享收藏感谢收起1110 条评论分享收藏感谢收起更多电脑内存占用高怎么办 Win7内存占用高解决办法 - Windows 7技巧 - 电脑百事网 - 专业的IT技术网站 关注手机、电脑、科技
手机扫描二维码
电脑内存占用高怎么办 Win7内存占用高解决办法
百事网原创
Win7系统在正常使用的时候会占用很多资源,比如网络诊断,缓存还有其他各种平时大部分时候我们都用不上的资源,如果我们电脑本身内存比较小只有2G内存或者以下,打开程序稍微多加个就会造成Win7内存占用高,整个系统运行缓慢,这是我们可以尝试将一些我们明确知道的不会使用的系统服务关系以释放被系统占用的内存。
当然还有较为保守的方法:,大家可以试试。
Win7内存占用高解决方法一
1:单击 按钮,在搜索框输入&服务&,然后在&程序&列表中选择&服务&选项。
2:打开服务窗口后,双击要停用的服务。
3:打开&属性&对话框后,在&常规&选项卡中的&描述&区域查看该服务的作用,然后打开&依存关系&选项卡,查看该服务与其他服务的依存关系,如图所示:
4在确定该服务不需使用而且停用后不会影响系统正常运行的情况下,在&常规&选项卡下禁用该服务。如图所示:
这里附上几个常用的Win7系统服务的中文含义,关于其他的你可以百度下,一般都有答案。
Print Spooler:这项服务主要为打印功能提供支持,如果电脑没有安装打印机并且不需要使用虚拟打印功能,可关闭该服务
Bluetooth Support Service:蓝牙功能支持,如果电脑不连接蓝牙设备,可以停用该服务。
Certificate Propagation:智能卡证书支持,对于未配置智能卡的电脑而言,该服务没有任何作用。
Adaptive brightness:监视氛围光传感器,以检测氛围光的变化并调节显示器的亮度。停止或禁用此服务后,显示器亮度将不根据照明条件进行调节。
Win7内存占用高解决方法二
1.启动&windows任务管理器&,快捷键&alt+ctrl+delete&点击&性能&选项卡,查看系统内存和物理内存的使用情况.
2.点击&开始&菜单(快捷键是Win键+R),然后再输入msconfig命令
3.会弹出&系统配置&的对话框,然后选择&服务&这个选项卡.你可以看到很多运行的服务.将前面的复选框的勾选上.然后点击&全部禁用&按钮.确定即可.(禁止服务时候.先确定 不是系统必须的服务(即禁用也不会影响系统的运行).
以上就是小编整理的关于电脑内存占用高的解决办法,我们主要是通过释放一些Win7系统保持运行的而我们日常使用时不会用上的系统服务。注意,一定要确认这些系统服务不是关键性系统服务,否则会导致操作系统不能正常运行。
加载更多内容
关注智能手机、电脑、笔记本、科技资讯, 致力于提供有价值的IT资讯。电脑内存突然占用过多是为什么? - 知乎79被浏览61655分享邀请回答11052 条评论分享收藏感谢收起47 条评论分享收藏感谢收起更多3 个回答被折叠()}

我要回帖

更多关于 内存占用不正常 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信