TestFullGCCount.java revision 2652
2652N/A/*
2652N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
2652N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2652N/A *
2652N/A * This code is free software; you can redistribute it and/or modify it
2652N/A * under the terms of the GNU General Public License version 2 only, as
2652N/A * published by the Free Software Foundation.
2652N/A *
2652N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2652N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2652N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2652N/A * version 2 for more details (a copy is included in the LICENSE file that
2652N/A * accompanied this code).
2652N/A *
2652N/A * You should have received a copy of the GNU General Public License version
2652N/A * 2 along with this work; if not, write to the Free Software Foundation,
2652N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2652N/A *
2652N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2652N/A * or visit www.oracle.com if you need additional information or have any
2652N/A * questions.
2652N/A */
2652N/A
2652N/A/*
2652N/A * @test TestFullGCount.java
2652N/A * @bug 7072527
2652N/A * @summary CMS: JMM GC counters overcount in some cases
2652N/A * @run main/othervm -XX:+UseConcMarkSweepGC TestFullGCCount
2652N/A *
2652N/A */
2652N/Aimport java.util.*;
2652N/Aimport java.lang.management.*;
2652N/A
2652N/Apublic class TestFullGCCount {
2652N/A
2652N/A public String collectorName = "ConcurrentMarkSweep";
2652N/A
2652N/A public static void main(String [] args) {
2652N/A
2652N/A TestFullGCCount t = null;
2652N/A if (args.length==2) {
2652N/A t = new TestFullGCCount(args[0], args[1]);
2652N/A } else {
2652N/A t = new TestFullGCCount();
2652N/A }
2652N/A System.out.println("Monitoring collector: " + t.collectorName);
2652N/A t.run();
2652N/A }
2652N/A
2652N/A public TestFullGCCount(String pool, String collector) {
2652N/A collectorName = collector;
2652N/A }
2652N/A
2652N/A public TestFullGCCount() {
2652N/A }
2652N/A
2652N/A public void run() {
2652N/A int count = 0;
2652N/A int iterations = 20;
2652N/A long counts[] = new long[iterations];
2652N/A boolean diffAlways2 = true; // assume we will fail
2652N/A
2652N/A for (int i=0; i<iterations; i++) {
2652N/A System.gc();
2652N/A counts[i] = getCollectionCount();
2652N/A if (i>0) {
2652N/A if (counts[i] - counts[i-1] != 2) {
2652N/A diffAlways2 = false;
2652N/A }
2652N/A }
2652N/A }
2652N/A if (diffAlways2) {
2652N/A throw new RuntimeException("FAILED: System.gc must be incrementing count twice.");
2652N/A }
2652N/A System.out.println("Passed.");
2652N/A }
2652N/A
2652N/A private long getCollectionCount() {
2652N/A long count = 0;
2652N/A List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
2652N/A List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
2652N/A for (int i=0; i<collectors.size(); i++) {
2652N/A GarbageCollectorMXBean collector = collectors.get(i);
2652N/A String name = collector.getName();
2652N/A if (name.contains(collectorName)) {
2652N/A System.out.println(name + ": collection count = "
2652N/A + collector.getCollectionCount());
2652N/A count = collector.getCollectionCount();
2652N/A }
2652N/A }
2652N/A return count;
2652N/A }
2652N/A
2652N/A}
2652N/A