All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.instrumentation.test.utils.GcUtils Maven / Gradle / Ivy

/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.instrumentation.test.utils;

import java.lang.ref.WeakReference;

public final class GcUtils {

  public static void awaitGc() throws InterruptedException {
    Object obj = new Object();
    WeakReference ref = new WeakReference<>(obj);
    obj = null;
    awaitGc(ref);
  }

  public static void awaitGc(WeakReference ref) throws InterruptedException {
    while (ref.get() != null) {
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      System.gc();
      System.runFinalization();
    }
  }

  private GcUtils() {}
}