blueprint.sdk.util.jvm.MemoryMonitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blueprint-sdk Show documentation
Show all versions of blueprint-sdk Show documentation
Personal library for Java development. Deployed on OSSRH for Apache Maven.
The newest version!
/*
License:
blueprint-sdk is licensed under the terms of Eclipse Public License(EPL) v1.0
(http://www.eclipse.org/legal/epl-v10.html)
Distribution:
Maven Central - https://search.maven.org/artifact/io.github.lempel/blueprint-sdk
MVN Repository - https://mvnrepository.com/artifact/io.github.lempel/blueprint-sdk
*/
package blueprint.sdk.util.jvm;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import blueprint.sdk.util.Terminatable;
/**
* Monitors memory usage
* For better result, please set -Xmx argument.
*
* @author [email protected]
* @since 2009. 3. 2.
*/
public class MemoryMonitor implements Terminatable, Runnable {
private static final Logger L = LoggerFactory.getLogger(MemoryMonitor.class);
/**
* check interval - 10sec
*/
private static final int INTERVAL = 10000;
/**
* memory usage limit to warn - 80%
*/
private static final int WARNING_USAGE = 80;
/**
* maximum tolerable warnings - 6times
*/
private static final int MAX_WARNINGS = 6;
private boolean running = false;
private transient boolean terminated = false;
private boolean trace = false;
@SuppressWarnings("WeakerAccess")
public MemoryMonitor() {
super();
}
@SuppressWarnings("WeakerAccess")
public MemoryMonitor(boolean trace) {
super();
this.trace = trace;
}
/**
* @return current heap usage in percent
*/
public static int getMemoryUsage() {
Runtime rtime = Runtime.getRuntime();
long total = rtime.maxMemory();
long used = rtime.totalMemory() - rtime.freeMemory();
double ratio = (double) used / (double) total;
return (int) (ratio * 100d);
}
/**
* @return true if -Xmx is set
*/
@SuppressWarnings("WeakerAccess")
public static boolean isXmxSet() {
boolean result = false;
RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
List arguments = RuntimemxBean.getInputArguments();
for (String arg : arguments) {
if (arg.toLowerCase().startsWith("-xmx")) {
result = true;
break;
}
}
return result;
}
public boolean isValid() {
return running;
}
public boolean isTerminated() {
return terminated;
}
public void terminate() {
running = false;
}
public void start() {
Thread thr = new Thread(this);
thr.setName(this.getClass().getName());
thr.setDaemon(true);
thr.start();
}
public void run() {
running = true;
boolean interrupted = false;
int warnCount = 0;
boolean xmx = isXmxSet();
while (running) {
try {
if (!interrupted) {
Runtime rtime = Runtime.getRuntime();
long total = rtime.maxMemory();
long used = rtime.totalMemory() - rtime.freeMemory();
double ratio = (double) used / (double) total;
int percent = (int) (ratio * 100d);
if (percent >= WARNING_USAGE) {
warnCount++;
if (warnCount >= MAX_WARNINGS) {
L.error("LOW MEMORY!! Memory usage is Critical. " + percent + "%");
if (xmx) {
L.error("RECOMMEND: 1. Increase -Xmx value");
} else {
L.error("RECOMMEND: 1. Set -Xmx value");
}
L.error("RECOMMEND: 2. Check for memory leak");
L.error("RECOMMEND: 3. Install more RAM");
warnCount = 0;
} else {
L.warn("Memory usage: " + percent + "% - " + (used / 1024 / 1024) + "M");
}
} else {
warnCount = 0;
}
if (trace) {
L.info("Memory usage: " + percent + "% - " + (used / 1024 / 1024) + "M");
}
}
} catch (OutOfMemoryError oom) {
L.error("OutOfMemoryError", oom);
}
try {
Thread.sleep(INTERVAL);
interrupted = false;
} catch (InterruptedException e) {
interrupted = true;
}
}
terminated = true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy