JDK-8242480 : Negative value may be returned by getFreeSwapSpaceSize() in the docker
  • Type: Bug
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: generic
  • Submitted: 2020-04-10
  • Updated: 2020-05-18
  • Resolved: 2020-04-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15 b20Fixed
Related Reports
Relates :  
Relates :  
Description
Negative values were returned by getFreeSwapSpaceSize() in our docker testing.
The reason is that current implementation doesn't consider the situation when memory.limit_in_bytes == memory.memsw.limit_in_bytes, which means do not use the swap space at all.

In src/jdk.management/unix/classes/com/sun/management/internal/OperatingSystemImpl.java, let's see
------------------------------------------------
 71     public long getFreeSwapSpaceSize() {
 72         if (containerMetrics != null) {
 73             long memSwapLimit = containerMetrics.getMemoryAndSwapLimit();
 74             long memLimit = containerMetrics.getMemoryLimit();
 75             if (memSwapLimit >= 0 && memLimit >= 0) {
 76                 for (int attempt = 0; attempt < MAX_ATTEMPTS_NUMBER; attempt++) {
 77                     long memSwapUsage = containerMetrics.getMemoryAndSwapUsage();
 78                     long memUsage = containerMetrics.getMemoryUsage();
 79                     if (memSwapUsage > 0 && memUsage > 0) {
 80                         // We read "memory usage" and "memory and swap usage" not atomically,
 81                         // and it's possible to get the negative value when subtracting these two.
 82                         // If this happens just retry the loop for a few iterations.
 83                         if ((memSwapUsage - memUsage) >= 0) {
 84                             return memSwapLimit - memLimit - (memSwapUsage - memUsage);
 85                         }
 86                     }
 87                 }
 88             }
 89         }
 90         return getFreeSwapSpaceSize0();
 91     }
------------------------------------------------
If memSwapLimit (@line 73) equals memLimit (@line 74), then getFreeSwapSpaceSize() may return a negative value @line 84.
Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/ed5d312d82b6 User: jiefu Date: 2020-04-18 01:12:28 +0000
18-04-2020

RFR: https://mail.openjdk.java.net/pipermail/serviceability-dev/2020-April/031067.html
10-04-2020