Browse Source

Improve awaitGc reliability (#11419)

Lauri Tulmin 9 months ago
parent
commit
bea670a345

+ 11 - 0
testing-common/src/main/java/io/opentelemetry/instrumentation/test/utils/GcUtils.java

@@ -12,6 +12,8 @@ import java.util.concurrent.TimeoutException;
 
 public final class GcUtils {
 
+  private static final StringBuilder garbage = new StringBuilder();
+
   public static void awaitGc(Duration timeout) throws InterruptedException, TimeoutException {
     Object obj = new Object();
     WeakReference<Object> ref = new WeakReference<>(obj);
@@ -27,6 +29,15 @@ public final class GcUtils {
       if (Thread.interrupted()) {
         throw new InterruptedException();
       }
+      // generate garbage to give gc some work
+      for (int i = 0; i < 26; i++) {
+        if (garbage.length() == 0) {
+          garbage.append("ab");
+        } else {
+          garbage.append(garbage);
+        }
+      }
+      garbage.setLength(0);
       System.gc();
     }
     if (ref.get() != null) {