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<Object>();
035    
036                    SoftReference<Object> softReference = new SoftReference<Object>(
037                            new Object(), referenceQueue);
038    
039                    List<byte[]> list = new ArrayList<byte[]>();
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                    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
066    
067                    WeakReference<Object> weakReference = new WeakReference<Object>(
068                            new Object(), referenceQueue);
069    
070                    while (weakReference.get() != null) {
071                            System.gc();
072    
073                            System.runFinalization();
074                    }
075    
076                    Assert.assertSame(weakReference, referenceQueue.remove());
077    
078                    if (ensureEnqueuedReferences) {
079                            gc(false);
080                    }
081            }
082    
083    }