Skip to content

Commit 14de791

Browse files
author
Alex Menkov
committed
8255934: JConsole 14 and greater fails to connect to older JVM
Reviewed-by: cjplummer, sspitsyn
1 parent 86f3602 commit 14de791

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/jdk.jconsole/share/classes/sun/tools/jconsole/SummaryTab.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.text.*;
3333
import java.util.*;
3434
import java.util.concurrent.*;
35+
import java.util.function.LongSupplier;
3536

3637
import javax.swing.*;
3738

@@ -257,9 +258,14 @@ synchronized Result formatSummary() {
257258
String[] kbStrings1 =
258259
formatKByteStrings(sunOSMBean.getCommittedVirtualMemorySize());
259260

261+
// getTotalPhysicalMemorySize and getFreePhysicalMemorySize are deprecated,
262+
// but we want be able to get the data for old target VMs (see JDK-8255934).
263+
@SuppressWarnings("deprecation")
260264
String[] kbStrings2 =
261-
formatKByteStrings(sunOSMBean.getTotalMemorySize(),
262-
sunOSMBean.getFreeMemorySize(),
265+
formatKByteStrings(tryToGet(sunOSMBean::getTotalMemorySize,
266+
sunOSMBean::getTotalPhysicalMemorySize),
267+
tryToGet(sunOSMBean::getFreeMemorySize,
268+
sunOSMBean::getFreePhysicalMemorySize),
263269
sunOSMBean.getTotalSwapSpaceSize(),
264270
sunOSMBean.getFreeSwapSpaceSize());
265271

@@ -317,6 +323,20 @@ synchronized Result formatSummary() {
317323
return result;
318324
}
319325

326+
/**
327+
* Tries to get the specified value from the list of suppliers.
328+
* Returns -1 if all suppliers fail.
329+
*/
330+
private long tryToGet(LongSupplier ... getters) {
331+
for (LongSupplier getter : getters) {
332+
try {
333+
return getter.getAsLong();
334+
} catch (UndeclaredThrowableException e) {
335+
}
336+
}
337+
return -1;
338+
}
339+
320340
private synchronized void append(String str) {
321341
buf.append(str);
322342
}

0 commit comments

Comments
 (0)