Use system properties to detect OpenJ9 JVMs when doing heapdumps

Closes gh-45973
This commit is contained in:
Moritz Halbritter 2025-06-18 10:16:25 +02:00
parent c13dc92286
commit 0f77dcb402
1 changed files with 17 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@ -46,6 +47,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* Web {@link Endpoint @Endpoint} to expose heap dumps.
@ -111,12 +113,22 @@ public class HeapDumpWebEndpoint {
* @throws HeapDumperUnavailableException if the heap dumper cannot be created
*/
protected HeapDumper createHeapDumper() throws HeapDumperUnavailableException {
try {
return new HotSpotDiagnosticMXBeanHeapDumper();
}
catch (HeapDumperUnavailableException ex) {
if (isRunningOnOpenJ9()) {
return new OpenJ9DiagnosticsMXBeanHeapDumper();
}
return new HotSpotDiagnosticMXBeanHeapDumper();
}
private boolean isRunningOnOpenJ9() {
String vmName = System.getProperty("java.vm.name");
if (StringUtils.hasLength(vmName) && vmName.toLowerCase(Locale.ROOT).contains("openj9")) {
return true;
}
String vmVendor = System.getProperty("java.vm.vendor");
if (StringUtils.hasLength(vmVendor) && vmVendor.toLowerCase(Locale.ROOT).contains("openj9")) {
return true;
}
return false;
}
/**