Skip to content

Commit

Permalink
Merge pull request #36 from KwaiAppTeam/dev
Browse files Browse the repository at this point in the history
Update wechat QRCode and merge latest dev branch
  • Loading branch information
AndroidInternal authored Oct 22, 2020
2 parents 3e865fc + 5125765 commit d13391b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ If you are interested in contributing, check out the [CONTRIBUTING.md](./CONTRIB
## Feedback
Welcome report [issues](https://github.com/KwaiAppTeam/KOOM/issues) or contact us in WeChat group.

<img src=./doc/images/wechat4.jpg/>
<img src=./doc/images/wechat5.jpg/>

2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ KOOM 以 Apache-2.0 证书开源,详情参见 [LICENSE](./LICENSE)。
## 问题 & 反馈
欢迎提 [issues](https://github.com/KwaiAppTeam/KOOM/issues) 提问反馈,或者加入微信群。

<img src=./doc/images/wechat4.jpg/>。
<img src=./doc/images/wechat5.jpg/>。
Binary file added doc/images/wechat5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions java-oom/src/main/java/com/kwai/koom/javaoom/common/KConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.kwai.koom.javaoom.common;

import static com.kwai.koom.javaoom.common.KGlobalConfig.KOOM_DIR;
import com.kwai.koom.javaoom.monitor.HeapThreshold;

import java.io.File;

import com.kwai.koom.javaoom.monitor.HeapThreshold;
import static com.kwai.koom.javaoom.common.KGlobalConfig.KOOM_DIR;

/**
* Copyright 2020 Kwai, Inc. All rights reserved.
Expand Down Expand Up @@ -59,6 +59,7 @@ public static KConfig defaultConfig() {
public static class KConfigBuilder {

private float heapRatio;
private float heapMaxRatio;
private int heapOverTimes;
private int heapPollInterval;

Expand All @@ -68,6 +69,7 @@ public static class KConfigBuilder {

public KConfigBuilder() {
this.heapRatio = KConstants.HeapThreshold.getDefaultPercentRation();
this.heapMaxRatio = KConstants.HeapThreshold.getDefaultMaxPercentRation();
this.heapOverTimes = KConstants.HeapThreshold.OVER_TIMES;
this.heapPollInterval = KConstants.HeapThreshold.POLL_INTERVAL;
File cacheFile = KGlobalConfig.getApplication().getCacheDir();
Expand All @@ -85,6 +87,11 @@ public KConfigBuilder heapRatio(float heapRatio) {
return this;
}

public KConfigBuilder heapMaxRatio(float heapMaxRatio) {
this.heapMaxRatio = heapMaxRatio;
return this;
}

public KConfigBuilder heapOverTimes(int heapOverTimes) {
this.heapOverTimes = heapOverTimes;
return this;
Expand All @@ -101,8 +108,11 @@ public KConfigBuilder processName(String name) {
}

public KConfig build() {
if (heapRatio > heapMaxRatio) {
throw new RuntimeException("heapMaxRatio be greater than heapRatio");
}
HeapThreshold heapThreshold = new HeapThreshold(heapRatio,
heapOverTimes, heapPollInterval);
heapMaxRatio, heapOverTimes, heapPollInterval);
return new KConfig(heapThreshold, this.rootDir, this.processName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static com.kwai.koom.javaoom.common.KConstants.Bytes.MB;

import android.util.Log;

/**
* Copyright 2020 Kwai, Inc. All rights reserved.
* <p>
Expand Down Expand Up @@ -38,6 +36,8 @@ public static class HeapThreshold {
public static float PERCENT_RATIO_IN_256_DEVICE = 85;
public static float PERCENT_RATIO_IN_128_DEVICE = 90;

public static float PERCENT_MAX_RATIO = 95;

public static float getDefaultPercentRation() {
int maxMem = (int) (Runtime.getRuntime().maxMemory() / MB);
if (Debug.VERBOSE_LOG) {
Expand All @@ -53,6 +53,10 @@ public static float getDefaultPercentRation() {
return KConstants.HeapThreshold.PERCENT_RATIO_IN_512_DEVICE;
}

public static float getDefaultMaxPercentRation() {
return KConstants.HeapThreshold.PERCENT_MAX_RATIO;
}

public static int OVER_TIMES = 3;
public static int POLL_INTERVAL = 5000;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public float value() {
return DEFAULT_FD_COUNT;
}

@Override
public float maxValue() {
return 0;
}

@Override
public int overTimes() {
return DEFAULT_OVER_TIMES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.kwai.koom.javaoom.monitor;

import android.util.Log;

import com.kwai.koom.javaoom.common.KConstants;
import com.kwai.koom.javaoom.common.KGlobalConfig;
import com.kwai.koom.javaoom.common.KLog;
Expand Down Expand Up @@ -57,12 +55,19 @@ public boolean isTrigger() {

HeapStatus heapStatus = currentHeapStatus();

if (heapStatus.isOverMaxThreshold) {
// 已达到最大阀值,强制触发trigger,防止后续出现大内存分配导致OOM进程Crash,无法触发trigger
KLog.i(TAG, "heap used is over max ratio, force trigger and over times reset to 0");
currentTimes = 0;
return true;
}

if (heapStatus.isOverThreshold) {
KLog.i(TAG, "heap status used:" + heapStatus.used / KConstants.Bytes.MB
+ ", max:" + heapStatus.max / KConstants.Bytes.MB
+ ", last over times:" + currentTimes);
+ ", max:" + heapStatus.max / KConstants.Bytes.MB
+ ", last over times:" + currentTimes);
if (heapThreshold.ascending()) {
if (lastHeapStatus == null || heapStatus.used >= lastHeapStatus.used) {
if (lastHeapStatus == null || heapStatus.used >= lastHeapStatus.used || heapStatus.isOverMaxThreshold) {
currentTimes++;
} else {
KLog.i(TAG, "heap status used is not ascending, and over times reset to 0");
Expand All @@ -86,14 +91,17 @@ private HeapStatus currentHeapStatus() {
heapStatus.max = Runtime.getRuntime().maxMemory();
heapStatus.used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
KLog.i(TAG, 100.0f * heapStatus.used / heapStatus.max + " " + heapThreshold.value());
heapStatus.isOverThreshold = 100.0f * heapStatus.used / heapStatus.max > heapThreshold.value();
float heapInPercent = 100.0f * heapStatus.used / heapStatus.max;
heapStatus.isOverThreshold = heapInPercent > heapThreshold.value();
heapStatus.isOverMaxThreshold = heapInPercent > heapThreshold.maxValue();
return heapStatus;
}

static class HeapStatus {
long max;
long used;
boolean isOverThreshold;
boolean isOverMaxThreshold;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public float value() {
return DEFAULT_THRASH_SIZE * KConstants.Bytes.MB;
}

@Override
public float maxValue() {
return 0;
}

@Override
public int overTimes() {
return DEFAULT_OVER_TIMES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
public class HeapThreshold implements Threshold {

private float heapRatioInPercent;
private float heapMaxRatioInPercent;
private int overTimes;
private int pollInterval;

public HeapThreshold(float heapRatioInPercent, int overTimes, int pollInterval) {
public HeapThreshold(float heapRatioInPercent, float heapMaxRatioInPercent, int overTimes, int pollInterval) {
this.heapRatioInPercent = heapRatioInPercent;
this.heapMaxRatioInPercent = heapMaxRatioInPercent;
this.overTimes = overTimes;
this.pollInterval = pollInterval;
}
Expand All @@ -34,6 +36,11 @@ public float value() {
return heapRatioInPercent;
}

@Override
public float maxValue() {
return heapMaxRatioInPercent;
}

@Override
public int overTimes() {
return overTimes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public float value() {
return DEFAULT_THREAD_COUNT;
}

@Override
public float maxValue() {
return 0;
}

@Override
public int overTimes() {
return DEFAULT_OVER_TIMES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
public interface Threshold {

/**
* @return value
* @return value 触发trigger的最低阀值,且必须命中策略heapOverTimes次才会真正触发
*/
float value();

/**
* @return maxValue 达到这个最大阀值,强制触发trigger
*/
float maxValue();

/**
* @return threshold crossed times
*/
Expand Down

0 comments on commit d13391b

Please sign in to comment.