use of java.lang.management.MemoryPoolMXBean in project nifi by apache.
the class MonitorMemory method onConfigured.
@OnScheduled
public void onConfigured(final ConfigurationContext config) throws InitializationException {
final String desiredMemoryPoolName = config.getProperty(MEMORY_POOL_PROPERTY).getValue();
final String thresholdValue = config.getProperty(THRESHOLD_PROPERTY).getValue().trim();
threshold = thresholdValue;
final Long reportingIntervalValue = config.getProperty(REPORTING_INTERVAL).asTimePeriod(TimeUnit.MILLISECONDS);
if (reportingIntervalValue == null) {
reportingIntervalMillis = config.getSchedulingPeriod(TimeUnit.MILLISECONDS);
} else {
reportingIntervalMillis = reportingIntervalValue;
}
final List<MemoryPoolMXBean> memoryPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
for (int i = 0; i < memoryPoolBeans.size() && monitoredBean == null; i++) {
MemoryPoolMXBean memoryPoolBean = memoryPoolBeans.get(i);
String memoryPoolName = memoryPoolBean.getName();
if (desiredMemoryPoolName.equals(memoryPoolName)) {
monitoredBean = memoryPoolBean;
if (memoryPoolBean.isCollectionUsageThresholdSupported()) {
long calculatedThreshold;
if (DATA_SIZE_PATTERN.matcher(thresholdValue).matches()) {
calculatedThreshold = DataUnit.parseDataSize(thresholdValue, DataUnit.B).longValue();
} else {
final String percentage = thresholdValue.substring(0, thresholdValue.length() - 1);
final double pct = Double.parseDouble(percentage) / 100D;
calculatedThreshold = (long) (monitoredBean.getUsage().getMax() * pct);
}
monitoredBean.setUsageThreshold(calculatedThreshold);
}
}
}
if (monitoredBean == null) {
throw new InitializationException("Found no JVM Memory Pool with name " + desiredMemoryPoolName + "; will not monitor Memory Pool");
}
}
use of java.lang.management.MemoryPoolMXBean in project nifi by apache.
the class MonitorMemory method onTrigger.
@Override
public void onTrigger(final ReportingContext context) {
final MemoryPoolMXBean bean = monitoredBean;
if (bean == null) {
return;
}
final MemoryUsage usage = bean.getUsage();
if (usage == null) {
getLogger().warn("{} could not determine memory usage for pool with name {}", new Object[] { this, context.getProperty(MEMORY_POOL_PROPERTY) });
return;
}
final double percentageUsed = (double) usage.getUsed() / (double) usage.getMax() * 100D;
if (bean.isUsageThresholdExceeded()) {
if (System.currentTimeMillis() < reportingIntervalMillis + lastReportTime && lastReportTime > 0L) {
return;
}
lastReportTime = System.currentTimeMillis();
lastValueWasExceeded = true;
final String message = String.format("Memory Pool '%1$s' has exceeded the configured Threshold of %2$s, having used %3$s / %4$s (%5$.2f%%)", bean.getName(), threshold, FormatUtils.formatDataSize(usage.getUsed()), FormatUtils.formatDataSize(usage.getMax()), percentageUsed);
getLogger().warn("{}", new Object[] { message });
} else if (lastValueWasExceeded) {
lastValueWasExceeded = false;
lastReportTime = System.currentTimeMillis();
final String message = String.format("Memory Pool '%1$s' is no longer exceeding the configured Threshold of %2$s; currently using %3$s / %4$s (%5$.2f%%)", bean.getName(), threshold, FormatUtils.formatDataSize(usage.getUsed()), FormatUtils.formatDataSize(usage.getMax()), percentageUsed);
getLogger().info("{}", new Object[] { message });
}
}
use of java.lang.management.MemoryPoolMXBean in project jdk8u_jdk by JetBrains.
the class JVM_MANAGEMENT_MIB_IMPL method findInCache.
/**
* WARNING: This should probably be moved to JvmMemPoolTableMetaImpl
**/
private int findInCache(SnmpTableHandler handler, String poolName) {
if (!(handler instanceof SnmpCachedData)) {
if (handler != null) {
final String err = "Bad class for JvmMemPoolTable datas: " + handler.getClass().getName();
log.error("getJvmMemPoolEntry", err);
}
return -1;
}
final SnmpCachedData data = (SnmpCachedData) handler;
final int len = data.datas.length;
for (int i = 0; i < data.datas.length; i++) {
final MemoryPoolMXBean pool = (MemoryPoolMXBean) data.datas[i];
if (poolName.equals(pool.getName()))
return i;
}
return -1;
}
use of java.lang.management.MemoryPoolMXBean in project jdk8u_jdk by JetBrains.
the class LastGCInfo method checkGcInfo.
private static void checkGcInfo(String name, GcInfo info) throws Exception {
System.out.println("GC statistic for : " + name);
System.out.print("GC #" + info.getId());
System.out.print(" start:" + info.getStartTime());
System.out.print(" end:" + info.getEndTime());
System.out.println(" (" + info.getDuration() + "ms)");
Map usage = info.getMemoryUsageBeforeGc();
List pnames = new ArrayList();
for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
String poolname = (String) entry.getKey();
pnames.add(poolname);
MemoryUsage busage = (MemoryUsage) entry.getValue();
MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
if (ausage == null) {
throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname);
}
System.out.println("Usage for pool " + poolname);
System.out.println(" Before GC: " + busage);
System.out.println(" After GC: " + ausage);
}
// check if memory usage for all memory pools are returned
List pools = ManagementFactory.getMemoryPoolMXBeans();
for (Iterator iter = pools.iterator(); iter.hasNext(); ) {
MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
if (!pnames.contains(p.getName())) {
throw new RuntimeException("GcInfo does not contain " + "memory usage for pool " + p.getName());
}
}
}
use of java.lang.management.MemoryPoolMXBean in project geode by apache.
the class HeapMemoryMonitor method startJVMThresholdListener.
/**
* Register with the JVM to get threshold events.
*
* Package private for testing.
*/
void startJVMThresholdListener() {
final MemoryPoolMXBean memoryPoolMXBean = getTenuredMemoryPoolMXBean();
// appropriate value.
if (!testDisableMemoryUpdates) {
memoryPoolMXBean.setCollectionUsageThreshold(1);
}
final long usageThreshold = memoryPoolMXBean.getUsageThreshold();
this.cache.getLoggerI18n().info(LocalizedStrings.HeapMemoryMonitor_OVERRIDDING_MEMORYPOOLMXBEAN_HEAP_0_NAME_1, new Object[] { usageThreshold, memoryPoolMXBean.getName() });
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
NotificationEmitter emitter = (NotificationEmitter) mbean;
emitter.addNotificationListener(this, null, null);
}
Aggregations