diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java index c611375274..6bfd8e6b57 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-reporting-tasks/src/main/java/org/apache/nifi/controller/MonitorMemory.java @@ -36,11 +36,11 @@ import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; -import java.util.stream.Collectors; /** * Reporting task used to monitor usage of memory after Garbage Collection has @@ -90,15 +90,23 @@ import java.util.stream.Collectors; + " that the memory pool is exceeding this threshold.") public class MonitorMemory extends AbstractReportingTask { + private static final List GC_OLD_GEN_POOLS = Collections.unmodifiableList(Arrays.asList("Tenured Gen", "PS Old Gen", "G1 Old Gen", "CMS Old Gen", "ZHeap")); private static final AllowableValue[] memPoolAllowableValues; + private static String defaultMemoryPool; static { // Only allow memory pool beans that support usage thresholds, otherwise we wouldn't report anything anyway - List memoryPoolBeans = ManagementFactory.getMemoryPoolMXBeans().stream().filter(MemoryPoolMXBean::isUsageThresholdSupported).collect(Collectors.toList()); - memPoolAllowableValues = new AllowableValue[memoryPoolBeans.size()]; - for (int i = 0; i < memPoolAllowableValues.length; i++) { - memPoolAllowableValues[i] = new AllowableValue(memoryPoolBeans.get(i).getName()); - } + memPoolAllowableValues = ManagementFactory.getMemoryPoolMXBeans() + .stream() + .filter(MemoryPoolMXBean::isUsageThresholdSupported) + .map(MemoryPoolMXBean::getName) + .map(AllowableValue::new) + .toArray(AllowableValue[]::new); + defaultMemoryPool = Arrays.stream(memPoolAllowableValues) + .map(AllowableValue::getValue) + .filter(GC_OLD_GEN_POOLS::contains) + .findFirst() + .orElse(null); } public static final PropertyDescriptor MEMORY_POOL_PROPERTY = new PropertyDescriptor.Builder() @@ -110,6 +118,7 @@ public class MonitorMemory extends AbstractReportingTask { + " running host platform and JVM") .required(true) .allowableValues(memPoolAllowableValues) + .defaultValue(defaultMemoryPool) .build(); public static final PropertyDescriptor THRESHOLD_PROPERTY = new PropertyDescriptor.Builder() .name("Usage Threshold")