com.mrcd.mmat.monitor.JavaHeapOOMMonitor Maven / Gradle / Ivy
The newest version!
package com.mrcd.mmat.monitor;
import com.mrcd.mmat.MMATConfig;
import com.mrcd.mmat.monitor.model.SystemInfo;
/**
*
Description: Java 堆内存监控
*
检测策略:1)当前 java heap 使用率 >= 配置的阈值 (且)
* 2)连续 N 次当前 java heap 堆内存占用率下降 <= gap,N >= 配置的阈值
*
*
Author: chenlin
*
Date: 2021/10/26
*/
public class JavaHeapOOMMonitor implements IMonitorJavaHeap{
//上次 java heap 内存占用率
private double mLastHeapRatio = 0d;
//连续超过阈值的次数
private int mOverThresholdCount = 0;
/**
*
Description: 检测 java 堆内存是否符合告警标准,符合返回 true,不符合返回 false
*
Author: chenlin
*
Date: 2021/10/26
* @param mmatConfig
*/
@Override
public boolean trackJavaHeap(MMATConfig mmatConfig){
boolean overThreshold = false;
double usedRate = SystemInfo.INSTANCE.getJavaHeap().getRate();
// String pattern1 = "TOTAL:";
// String pattern2 = "TOTALSWAPPSS:";
// if(result.contains(pattern1) && result.contains(pattern2)){
// int firstIndex = pattern1.length();
// int lastIndex = result.indexOf(pattern2);
// String total = result.substring(firstIndex,lastIndex);
// //当前进程占用的物理内存,单位 KB
// long totalPss = Long.parseLong(total);
// System.out.println("当前进程占用的物理内存:"+ UnitUtil.kB2MStdOut(totalPss));
//
// overThreshold = true;
// }else {
// System.out.println("结果不符合规范,结束此次检测");
// overThreshold = false;
// }
if ( usedRate >= mmatConfig.javaHeapUsageWarnThreshold &&
//本次内存占用率下降的幅度小于 gap
usedRate >= mLastHeapRatio - mmatConfig.javaHeapUsageWarnCountThreshold) {
mOverThresholdCount++;
System.out.println("Java Heap 监控模块当前告警统计数:"+mOverThresholdCount);
if(mOverThresholdCount >= mmatConfig.javaHeapUsageWarnCountThreshold){
overThreshold = true;
}
}else {
System.out.println("不符合告警标准,真棒 ^_^");
reset();
}
mLastHeapRatio = usedRate;
return overThreshold;
}
@Override
public void reset(){
mLastHeapRatio = 0d;
mOverThresholdCount = 0;
}
@Override
public String reason() {
return "reason_heap_oom";
}
}