/* * Copyright (c) 2017, Google and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package MyPackage; import java.util.List; /** * @test * @summary Checks the Recent garbage storage system. * @build Frame HeapMonitor * @compile HeapMonitorRecentTest.java * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorRecentTest */ public class HeapMonitorRecentTest { private native static boolean framesNotInLiveOrRecent(Frame[] frames); private native static boolean framesExistInLiveAndRecent(Frame[] frames); private static List runner(int max) { List frameList = null; for (int j = 0; j < max; j++) { frameList = HeapMonitor.allocate(); } frameList.add(new Frame("runner", "(I)Ljava/util/List;", "HeapMonitorRecentTest.java", 44)); return frameList; } public static void main(String[] args) { HeapMonitor.enableSampling(); // We are testing for the recent garbage sampler: // First run for 10000 iterations to fill up the garbage sampler. List firstFrameList = runner(10); firstFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorRecentTest.java", 54)); // Now because we are in a different stack frame line here, we can just re-use the same runner. // Run for 3, we really should not see that many of these and most should be the first type. List secondFrameList = runner(10); secondFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorRecentTest.java", 59)); // We should no longer have the initial frames. boolean status = framesNotInLiveOrRecent(firstFrameList.toArray(new Frame[0])); if (!status) { throw new RuntimeException("Initial frames still internally there."); } // We should see those new frames. status = framesExistInLiveAndRecent(secondFrameList.toArray(new Frame[0])); if (!status) { throw new RuntimeException("Second frame list not found in both live and recent."); } } }