1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import org.testng.annotations.Test; 25 import org.testng.Assert; 26 27 import java.util.concurrent.TimeUnit; 28 import java.util.concurrent.locks.Condition; 29 import java.util.concurrent.locks.ReentrantLock; 30 31 import com.oracle.java.testlibrary.dcmd.CommandExecutor; 32 import com.oracle.java.testlibrary.dcmd.JMXExecutor; 33 34 /* 35 * @test 36 * @summary Test of diagnostic command GC.run_finalization 37 * @library /testlibrary 38 * @modules java.base/sun.misc 39 * java.compiler 40 * java.management 41 * jdk.jvmstat/sun.jvmstat.monitor 42 * @build com.oracle.java.testlibrary.* 43 * @build com.oracle.java.testlibrary.dcmd.* 44 * @run testng RunFinalizationTest 45 */ 46 public class RunFinalizationTest { 47 static ReentrantLock lock = new ReentrantLock(); 48 static Condition cond = lock.newCondition(); 49 static volatile boolean wasFinalized = false; 50 static volatile boolean wasInitialized = false; 51 52 class MyObject { 53 public MyObject() { 54 /* Make sure object allocation/deallocation is not optimized out */ 55 wasInitialized = true; 56 } 57 58 protected void finalize() { 59 lock.lock(); 60 wasFinalized = true; 61 cond.signalAll(); 62 lock.unlock(); 63 } 64 } 65 66 public static MyObject o; 67 68 public void run(CommandExecutor executor) { 69 lock.lock(); 70 o = new MyObject(); 71 o = null; 72 System.gc(); 73 executor.execute("GC.run_finalization"); 74 75 int waited = 0; 76 int waitTime = 15; 77 78 try { 79 System.out.println("Waiting for signal from finalizer"); 80 81 while (!cond.await(waitTime, TimeUnit.SECONDS)) { 82 waited += waitTime; 83 System.out.println(String.format("Waited %d seconds", waited)); 84 } 85 86 System.out.println("Received signal"); 87 } catch (InterruptedException e) { 88 Assert.fail("Test error: Interrupted while waiting for signal from finalizer", e); 89 } finally { 90 lock.unlock(); 91 } 92 93 if (!wasFinalized) { 94 Assert.fail("Test failure: Object was not finalized"); 95 } 96 } 97 98 @Test 99 public void jmx() { 100 run(new JMXExecutor()); 101 } 102 }