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    *      http://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 java.io.Closeable;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.concurrent.CountDownLatch;
24  import java.util.concurrent.TimeUnit;
25  import java.util.concurrent.atomic.AtomicBoolean;
26  
27  // after: https://github.com/apache/logging-log4j2/blob/c47e98423b461731f7791fcb9ea1079cd451f365/log4j-core/src/test/java/org/apache/logging/log4j/core/GarbageCollectionHelper.java
28  public final class GarbageCollectionHelper implements Closeable, Runnable {
29      final class GcTask implements Runnable {
30          @Override
31          public void run() {
32              try {
33                  while (running.get()) {
34                      // Allocate data to help suggest a GC
35                      try {
36                          // 1mb of heap
37                          final byte[] buf = new byte[1024 * 1024];
38                          SINK.write(buf);
39                      } catch (final IOException ignored) {
40                      }
41                      // May no-op depending on the JVM configuration
42                      System.gc();
43                  }
44              } finally {
45                  latch.countDown();
46              }
47          }
48      }
49      private static final OutputStream SINK = new OutputStream() {
50          @Override
51          public void write(final byte[] b) {
52          }
53  
54          @Override
55          public void write(final byte[] b, final int off, final int len) {
56          }
57  
58          @Override
59          public void write(final int b) {
60          }
61      };
62      private final AtomicBoolean running = new AtomicBoolean();
63      private final CountDownLatch latch = new CountDownLatch(1);
64  
65      private final Thread gcThread = new Thread(new GcTask());
66  
67      @Override
68      public void close() {
69          running.set(false);
70          try {
71              junit.framework.TestCase.assertTrue("GarbageCollectionHelper did not shut down cleanly",
72                      latch.await(10, TimeUnit.SECONDS));
73          } catch (final InterruptedException e) {
74              throw new RuntimeException(e);
75          }
76      }
77  
78      @Override
79      public void run() {
80          if (running.compareAndSet(false, true)) {
81              gcThread.start();
82          }
83      }
84  }
85