public static long findProcessRssInKb()

in common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java [63:104]


    public static long findProcessRssInKb(long pid) {
        final Os os = Os.current();
        if (os.isUnixLike()) {
            String[] cmd = {"ps", "-o", "rss=", "-p", String.valueOf(pid)};
            final List<String> output = new ArrayList<>(1);
            exec(cmd, output);
            if (output.size() == 1) {
                try {
                    return Long.parseLong(output.get(0).trim());
                } catch (NumberFormatException e) {
                    LOGGER.warn(
                            "Could not parse the output of {} as a long:\n{}",
                            String.join(" ", cmd),
                            String.join("\n", output));
                }
            } else {
                LOGGER.warn("Unexpected output of {}:\n{}", String.join(" ", cmd), String.join("\n", output));
            }
            return -1;
        } else if (os == Os.WINDOWS) {
            String[] cmd = {"wmic", "process", "where", "processid=" + pid, "get", "WorkingSetSize"};
            final List<String> output = new ArrayList<>(1);
            exec(cmd, output);
            final List<String> nonEmptyLines =
                    output.stream().filter(l -> !l.isEmpty()).collect(Collectors.toList());
            if (nonEmptyLines.size() >= 2) {
                try {
                    return Long.parseLong(nonEmptyLines.get(1).trim()) / KB;
                } catch (NumberFormatException e) {
                    LOGGER.warn(
                            "Could not parse the second line of {} output as a long:\n{}",
                            String.join(" ", cmd),
                            String.join("\n", nonEmptyLines));
                }
            } else {
                LOGGER.warn("Unexpected output of {}:\n{}", String.join(" ", cmd), String.join("\n", output));
            }
            return -1;
        } else {
            return -1;
        }
    }