001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.test;
016    
017    import java.lang.ref.ReferenceQueue;
018    import java.lang.ref.SoftReference;
019    import java.lang.ref.WeakReference;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import org.junit.Assert;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class GCUtil {
030    
031            public static void fullGC(boolean ensureEnqueuedReferences)
032                    throws InterruptedException {
033    
034                    fullGC(true, ensureEnqueuedReferences);
035            }
036    
037            public static void fullGC(
038                            boolean actively, boolean ensureEnqueuedReferences)
039                    throws InterruptedException {
040    
041                    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
042    
043                    SoftReference<Object> softReference = new SoftReference<>(
044                            new Object(), referenceQueue);
045    
046                    List<byte[]> list = new ArrayList<>();
047    
048                    while (true) {
049                            try {
050                                    list.add(new byte[100 * 1024 * 1024]);
051                            }
052                            catch (OutOfMemoryError oome) {
053                                    list.clear();
054    
055                                    list = null;
056    
057                                    break;
058                            }
059                    }
060    
061                    Assert.assertNull(softReference.get());
062                    Assert.assertSame(softReference, referenceQueue.remove());
063    
064                    if (ensureEnqueuedReferences) {
065                            fullGC(actively, false);
066                    }
067            }
068    
069            public static void gc(boolean ensureEnqueuedReferences)
070                    throws InterruptedException {
071    
072                    gc(true, ensureEnqueuedReferences);
073            }
074    
075            public static void gc(boolean actively, boolean ensureEnqueuedReferences)
076                    throws InterruptedException {
077    
078                    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
079    
080                    WeakReference<Object> weakReference = new WeakReference<>(
081                            new Object(), referenceQueue);
082    
083                    if (actively) {
084                            while (weakReference.get() != null) {
085                                    System.gc();
086    
087                                    System.runFinalization();
088                            }
089                    }
090    
091                    Assert.assertSame(weakReference, referenceQueue.remove());
092    
093                    if (ensureEnqueuedReferences) {
094                            gc(actively, false);
095                    }
096            }
097    
098    }