@TestOnly
  public void ensureCollected(@NotNull Runnable runWhileWaiting) throws IllegalStateException {
    StringBuilder log = new StringBuilder();
    if (GCUtil.allocateTonsOfMemory(log, runWhileWaiting, this::isEverythingCollected)) {
      return;
    }

    String message = "Couldn't garbage-collect some objects, they might still be reachable from GC roots: " +
                     ContainerUtil.mapNotNull(myReferences, SoftReference::dereference);

    try {
      Path file = Paths.get(System.getProperty("teamcity.build.tempDir", System.getProperty("java.io.tmpdir")), "GCWatcher.hprof.zip");
      MemoryDumpHelper.captureMemoryDumpZipped(file);

      //noinspection UseOfSystemOutOrSystemErr
      System.out.println("##teamcity[publishArtifacts '" + file + "']");
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
    if (isEverythingCollected()) {
      message += "\nEverything is collected after taking the heap dump.";
    }
    message += " Log:\n" + log;
    throw new IllegalStateException(message);
  }
@TestOnly
  public void ensureCollected(@NotNull Runnable runWhileWaiting)