View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.commons.logging;
19  
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.Closeable;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.util.concurrent.CountDownLatch;
26  import java.util.concurrent.TimeUnit;
27  import java.util.concurrent.atomic.AtomicBoolean;
28  
29  // after: https://github.com/apache/logging-log4j2/blob/c47e98423b461731f7791fcb9ea1079cd451f365/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java
30  public final class GarbageCollectionHelper implements Closeable, Runnable {
31  
32      final class GcTask implements Runnable {
33          @Override
34          public void run() {
35              try {
36                  while (running.get()) {
37                      // Allocate data to help suggest a GC
38                      try {
39                          // 1mb of heap
40                          final byte[] buf = new byte[1024 * 1024];
41                          SINK.write(buf);
42                      } catch (final IOException ignored) {
43                      }
44                      // May no-op depending on the JVM configuration
45                      System.gc();
46                  }
47              } finally {
48                  latch.countDown();
49              }
50          }
51      }
52      private static final OutputStream SINK = new OutputStream() {
53          @Override
54          public void write(final byte[] b) {
55          }
56  
57          @Override
58          public void write(final byte[] b, final int off, final int len) {
59          }
60  
61          @Override
62          public void write(final int b) {
63          }
64      };
65  
66      private final AtomicBoolean running = new AtomicBoolean();
67      private final CountDownLatch latch = new CountDownLatch(1);
68      private final Thread gcThread = new Thread(new GcTask());
69  
70      @Override
71      public void close() {
72          running.set(false);
73          try {
74              assertTrue(latch.await(10, TimeUnit.SECONDS), "GarbageCollectionHelper did not shut down cleanly");
75          } catch (final InterruptedException e) {
76              Thread.currentThread().interrupt();
77              throw new RuntimeException(e);
78          }
79      }
80  
81      @Override
82      public void run() {
83          if (running.compareAndSet(false, true)) {
84              gcThread.start();
85          }
86      }
87  }
88