private long getTotalPhysicalMemoryUsageBytes() throws IOException {
    // collect all child process ids of the main process that runs the application
    List<String> processIds = getAllCommandOutput(new String[]{"sh", "-c", "pgrep -P $PPID"});
    // add the parent process which is the main process that runs the application
    processIds.add("$PPID");
    String processIdsJoined = String.join(" ", processIds);
    // returns a list of long values that represent the rss memory of each process.
    List<String> processMemoryKBArray = getAllCommandOutput(new String[]{"sh", "-c", String.format("ps -o rss= -p %s", processIdsJoined)});
    long totalPhysicalMemoryKB = 0;
    for (String processMemory : processMemoryKBArray) {
      totalPhysicalMemoryKB += Long.parseLong(processMemory.trim());
    }
    //convert to bytes
    return totalPhysicalMemoryKB * 1024;
  }
A convenience method to execute shell commands and return all lines of their output.

@param cmdArray the command to run
@return all lines of the output.
@throws IOException
samza-core/src/main/java/org/apache/samza/container/host/PosixCommandBasedStatisticsGetter.java