io.micrometer.core.instrument.binder.jvm.JvmMemory Maven / Gradle / Ivy
Show all versions of micrometer-core Show documentation
/**
* Copyright 2019 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.jvm;
import io.micrometer.core.lang.Nullable;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.lang.management.MemoryUsage;
import java.util.Optional;
import java.util.function.ToLongFunction;
class JvmMemory {
private JvmMemory() {
}
static Optional getLongLivedHeapPool() {
return ManagementFactory
.getPlatformMXBeans(MemoryPoolMXBean.class)
.stream()
.filter(JvmMemory::isHeap)
.filter(mem -> isOldGenPool(mem.getName()) || isNonGenerationalHeapPool(mem.getName()))
.findAny();
}
static boolean isConcurrentPhase(String cause, String name) {
return "No GC".equals(cause)
|| "Shenandoah Cycles".equals(name);
}
static boolean isAllocationPool(String name) {
return name != null && name.endsWith("Eden Space");
}
static boolean isOldGenPool(String name) {
return name != null && (name.endsWith("Old Gen") || name.endsWith("Tenured Gen"));
}
static boolean isNonGenerationalHeapPool(String name) {
return "Shenandoah".equals(name) || "ZHeap".equals(name);
}
private static boolean isHeap(MemoryPoolMXBean memoryPoolBean) {
return MemoryType.HEAP.equals(memoryPoolBean.getType());
}
static double getUsageValue(MemoryPoolMXBean memoryPoolMXBean, ToLongFunction getter) {
MemoryUsage usage = getUsage(memoryPoolMXBean);
if (usage == null) {
return Double.NaN;
}
return getter.applyAsLong(usage);
}
@Nullable
private static MemoryUsage getUsage(MemoryPoolMXBean memoryPoolMXBean) {
try {
return memoryPoolMXBean.getUsage();
} catch (InternalError e) {
// Defensive for potential InternalError with some specific JVM options. Based on its Javadoc,
// MemoryPoolMXBean.getUsage() should return null, not throwing InternalError, so it seems to be a JVM bug.
return null;
}
}
}