Skip to content

Commit 18c9cff

Browse files
committed
Use reflection to compute total memory size
In performance tool. This avoids a deprecation warning when compiling on Java 14+.
1 parent a130883 commit 18c9cff

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/main/java/com/rabbitmq/stream/perf/Utils.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.rabbitmq.stream.OffsetSpecification;
1818
import com.rabbitmq.stream.StreamCreator.LeaderLocator;
1919
import com.rabbitmq.stream.compression.Compression;
20+
import com.sun.management.OperatingSystemMXBean;
21+
import java.lang.reflect.Method;
2022
import java.security.cert.X509Certificate;
2123
import java.text.CharacterIterator;
2224
import java.text.StringCharacterIterator;
@@ -36,6 +38,7 @@
3638
import java.util.concurrent.ThreadFactory;
3739
import java.util.concurrent.atomic.AtomicLong;
3840
import java.util.function.BiFunction;
41+
import java.util.function.LongSupplier;
3942
import java.util.stream.Collectors;
4043
import java.util.stream.IntStream;
4144
import javax.net.ssl.SNIHostName;
@@ -49,10 +52,38 @@
4952
class Utils {
5053

5154
static final X509TrustManager TRUST_EVERYTHING_TRUST_MANAGER = new TrustEverythingTrustManager();
55+
private static final LongSupplier TOTAL_MEMORY_SIZE_SUPPLIER;
5256
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
5357
private static final String RANGE_SEPARATOR_1 = "-";
5458
private static final String RANGE_SEPARATOR_2 = "..";
5559

60+
// this trick avoids a deprecation warning when compiling on Java 14+
61+
static {
62+
Method method;
63+
try {
64+
// Java 14+
65+
method = OperatingSystemMXBean.class.getDeclaredMethod("getTotalMemorySize");
66+
} catch (NoSuchMethodException nsme) {
67+
try {
68+
method = OperatingSystemMXBean.class.getDeclaredMethod("getTotalPhysicalMemorySize");
69+
} catch (Exception e) {
70+
throw new RuntimeException("Error while computing method to get total memory size");
71+
}
72+
}
73+
Method m = method;
74+
TOTAL_MEMORY_SIZE_SUPPLIER =
75+
() -> {
76+
OperatingSystemMXBean os =
77+
(OperatingSystemMXBean)
78+
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
79+
try {
80+
return (long) m.invoke(os);
81+
} catch (Exception e) {
82+
throw new RuntimeException("Could not retrieve total memory size", e);
83+
}
84+
};
85+
}
86+
5687
static void writeLong(byte[] array, long value) {
5788
// from Guava Longs
5889
for (int i = 7; i >= 0; i--) {
@@ -123,10 +154,7 @@ static String formatByte(double bytes) {
123154

124155
static long physicalMemory() {
125156
try {
126-
com.sun.management.OperatingSystemMXBean os =
127-
(com.sun.management.OperatingSystemMXBean)
128-
java.lang.management.ManagementFactory.getOperatingSystemMXBean();
129-
return os.getTotalPhysicalMemorySize();
157+
return TOTAL_MEMORY_SIZE_SUPPLIER.getAsLong();
130158
} catch (Throwable e) {
131159
// we can get NoClassDefFoundError, so we catch from Throwable and below
132160
LOGGER.warn("Could not get physical memory", e);

0 commit comments

Comments
 (0)