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                    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
035    
036                    SoftReference<Object> softReference = new SoftReference<>(
037                            new Object(), referenceQueue);
038    
039                    List<byte[]> list = new ArrayList<>();
040    
041                    while (true) {
042                            try {
043                                    list.add(new byte[100 * 1024 * 1024]);
044                            }
045                            catch (OutOfMemoryError oome) {
046                                    list.clear();
047    
048                                    list = null;
049    
050                                    break;
051                            }
052                    }
053    
054                    Assert.assertNull(softReference.get());
055                    Assert.assertSame(softReference, referenceQueue.remove());
056    
057                    if (ensureEnqueuedReferences) {
058                            fullGC(false);
059                    }
060            }
061    
062            public static void gc(boolean ensureEnqueuedReferences)
063                    throws InterruptedException {
064    
065                    gc(true, ensureEnqueuedReferences);
066            }
067    
068            public static void gc(boolean actively, boolean ensureEnqueuedReferences)
069                    throws InterruptedException {
070    
071                    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
072    
073                    WeakReference<Object> weakReference = new WeakReference<>(
074                            new Object(), referenceQueue);
075    
076                    if (actively) {
077                            while (weakReference.get() != null) {
078                                    System.gc();
079    
080                                    System.runFinalization();
081                            }
082                    }
083    
084                    Assert.assertSame(weakReference, referenceQueue.remove());
085    
086                    if (ensureEnqueuedReferences) {
087                            gc(actively, false);
088                    }
089            }
090    
091    }