com.mrcd.mmat.monitor.FastBigJavaHeapOOMMonitor.kt 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 heap 急速激增 OOM 监控,策略
* 1)本次 java heap 内存占用率达到最高告警阈值 (或)
* 2)本次 java heap 内存占用率比上次的高了最高差值阈值
*
*
*
Author: chenlin
*
Date: 2021/10/28
*/
class FastBigJavaHeapOOMMonitor : IMonitorJavaHeap{
companion object {
private const val REASON_MAX_USAGE_THRESHOLD = "max_usage_threshold"
private const val REASON_MAX_GAP_THRESHOLD = "max_gap_threshold"
}
private var mDumpReason = ""
override fun trackJavaHeap(mmatConfig: MMATConfig): Boolean {
val javaHeap = SystemInfo.javaHeap
// 危险占用率阈值,直接触发dump分析
if (javaHeap.rate > mmatConfig.javaHeapDangerousUsageThreshold) {
mDumpReason = REASON_MAX_USAGE_THRESHOLD
println("java heap dangerous usage ==> dump")
return true
}else{
println("below java heap dangerous usage ^_^ ")
}
// 高差值直接dump
val lastJavaHeap = SystemInfo.lastJavaHeap
if (lastJavaHeap.max != 0L && javaHeap.used - lastJavaHeap.used > mmatConfig.javaHeapDangerousGapThreshold) {
mDumpReason = REASON_MAX_GAP_THRESHOLD
println("java heap dangerous gap,huge memory allocated ==> dump")
return true
}else{
println("below java heap dangerous gap ^_^ ")
}
return false
}
override fun reset() {
mDumpReason = ""
}
override fun reason(): String {
return "fast_big_mem_$mDumpReason"
}
}