private void checkDiskSpace(final String type, final File newFile, final long spaceLimit,
      final File dir, final Pattern pattern, final LogWriter logger) {
    if (spaceLimit == 0 || pattern == null) {
      return;
    }
    File[] children = findChildrenExcept(dir, pattern, newFile);
    LogWriter logWriter = logger;
    if (children == null) {
      if (dir.isDirectory()) {
        logWriter.warning(
            String.format(
                "Could not check disk space on %s because java.io.File.listFiles returned null. This could be caused by a lack of file descriptors.",
                dir));
      }
      return;
    }
    Arrays.sort(children, (Comparator) (o1, o2) -> {
      File f1 = (File) o1;
      File f2 = (File) o2;
      long diff = f1.lastModified() - f2.lastModified();
      if (diff < 0) {
        return -1;
      } else if (diff > 0) {
        return 1;
      } else {
        return 0;
      }
    });
    long spaceUsed = 0;
    for (File child : children) {
      spaceUsed += child.length();
    }
    int idx = 0;
    while (spaceUsed >= spaceLimit && idx < children.length) { // check array index to 37388
      long childSize = children[idx].length();
      if (delete(children[idx])) {
        spaceUsed -= childSize;
        logWriter.info(String.format("Deleted inactive %s %s.",
            type, children[idx]));
      } else {
        logWriter.warning(String.format("Could not delete inactive %s %s.",
            type, children[idx]));
      }
      idx++;
    }
    if (spaceUsed > spaceLimit) {
      logWriter.warning(
          String.format(
              "Could not free space in %s directory.  The space used is %s which exceeds the configured limit of %s.",
              type, spaceUsed, spaceLimit));
    }
  }
Extracted from ManagerLogWriter. MainWithChildrenRollingFileHandler is used for both rolling of
log files and stat archive files.
geode-core/src/main/java/org/apache/geode/internal/io/MainWithChildrenRollingFileHandler.java