Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/FinalizeTest01/FinalizeTest01.java
41159 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress randomness26*27* @summary converted from VM Testbase gc/gctests/FinalizeTest01.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, monitoring]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.FinalizeTest01.FinalizeTest0133*/3435package gc.gctests.FinalizeTest01;3637import java.lang.management.ManagementFactory;38import java.lang.management.MemoryMXBean;39import nsk.share.gc.*;40import nsk.share.TestFailure;41import nsk.share.test.ExecutionController;42import nsk.share.test.Stresser;4344/**45* Tests that GC works correctly with finalizers.46*47* Create a number of objects, some of which register their48* creation/finalization. Then try to eat available memory49* which forces garbage collection of created objects.50* Garbage collector should try to collect the garbage and thus51* call finalizers. The test checks that all finalizers have been called.52*/5354/*55Reworked original test (4933478). Original comment:5657Tests that objects w/finalizers will eventually finalize58(and by the way, the system doesn't crash!).5960Returns exit code 0 on success, and 1 on failure.6162*/63/**64* Class with finalizer that throws Exception.65* Used for FinalizeTest0266*/67class FinExceptMemoryObject extends FinMemoryObject {6869public FinExceptMemoryObject(int size) {70super(size);71}7273protected void finalize() {74super.finalize();75throw new RuntimeException("Exception in finalizer");76}77}7879public class FinalizeTest01 extends GCTestBase {8081private final int allocRatio = 5;82private final int size = 1024 * 2;83private int count = 1000;84private static boolean throwExceptions = false;85private ExecutionController stresser;8687private void runOne() {88Object o;89for (int i = 0; i < count; i++) {90if (i % allocRatio == 0) {91if (throwExceptions) {92o = new FinExceptMemoryObject(size);93} else {94o = new FinMemoryObject(size);95}96} else {97o = new byte[size - Memory.getObjectExtraSize()];98}99}100o = null;101102MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();103long finalizationMaxTime = 1000 * 60; // 1min104105/* Provoke GC to start finalization. */106Algorithms.eatMemory(stresser);107if (!stresser.continueExecution()) {108// we did not eat all memory109return;110}111long waitTime = System.currentTimeMillis() + finalizationMaxTime;112113/*114* Before we force finalization it is needed to check that we have115* any object pending for finazlization. If not then is a GC bug.116*/117while (FinMemoryObject.getFinalizedCount()118+ mbean.getObjectPendingFinalizationCount() == 0119&& (System.currentTimeMillis() < waitTime)) {120System.out.println("No objects are found in the finalization queue. Waiting..");121try {122Thread.sleep(1000);123} catch (InterruptedException ie) {124}125}126if (FinMemoryObject.getFinalizedCount()127+ mbean.getObjectPendingFinalizationCount() == 0) {128throw new TestFailure("Test failed. (No objects were not queued for finalization during 1min)");129}130131/* force finalization and wait for it finishs */132Runtime.getRuntime().runFinalization();133134boolean error = (FinMemoryObject.getLiveCount() != 0);135136/*137* The runFinalization() starts the second finalization thread and wait until it finishs.138* However it is a very little probability (less then 1%) that not all object are finalized yet.139* Possibly the regular Finalizer thread have not finished its work when we check getLiveCount()140* or GC is still clearing memory and adding objects to the queue.141*/142waitTime = System.currentTimeMillis() + finalizationMaxTime;143while (error && (System.currentTimeMillis() < waitTime)) {144// wait 1 sec (it could be less due to potential InterruptedException)145try {146Thread.sleep(1000);147} catch (InterruptedException ie) {148}149error = (FinMemoryObject.getLiveCount() != 0);150}151152if (error) {153throw new TestFailure("Test failed (objects were not finalized during 1min)");154}155156157System.out.println("Allocated: " + FinMemoryObject.getAllocatedCount());158System.out.println("Finalized: " + FinMemoryObject.getFinalizedCount());159error = (FinMemoryObject.getLiveCount() != 0);160if (error) {161throw new TestFailure("Test failed.");162}163}164165public void run() {166stresser = new Stresser(runParams.getStressOptions());167stresser.start(runParams.getIterations());168count = (int) Math.min(runParams.getTestMemory() / size, Integer.MAX_VALUE);169System.out.println("Allocating " + count170+ " objects. 1 out of " + allocRatio171+ " will have a finalizer.");172System.out.flush();173runOne();174}175176public static void main(String[] args) {177for (int i = 0; i < args.length; ++i) {178if (args[i].equals("-throwExceptions")) {179throwExceptions = true;180}181}182GC.runTest(new FinalizeTest01(), args);183}184}185186187