2
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27
* @test TestFullGCCount.java
29
* @summary JMM GC counters overcount in some cases
30
* @comment Shenandoah has "ExplicitGCInvokesConcurrent" on by default
31
* @requires !(vm.gc == "Shenandoah" & vm.opt.ExplicitGCInvokesConcurrent != false)
32
* @comment G1 has separate counters for STW Full GC and concurrent GC.
33
* @requires !(vm.gc.G1 & vm.opt.ExplicitGCInvokesConcurrent == true)
34
* @requires vm.gc != "Z"
35
* @modules java.management
36
* @run main/othervm -Xlog:gc gc.TestFullGCCount
39
import java.lang.management.GarbageCollectorMXBean;
40
import java.lang.management.ManagementFactory;
41
import java.util.ArrayList;
42
import java.util.HashMap;
46
* Originally for a specific failure in CMS[[keep]], this test now monitors all
47
* collectors for double-counting of collections.
49
public class TestFullGCCount {
51
static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
53
public static void main(String[] args) {
55
boolean failed = false;
56
String errorMessage = "";
57
HashMap<String, List<Long>> counts = new HashMap<>();
59
// Prime the collection of count lists for all collectors.
60
for (int i = 0; i < collectors.size(); i++) {
61
GarbageCollectorMXBean collector = collectors.get(i);
62
counts.put(collector.getName(), new ArrayList<>(iterations));
65
// Perform some gc, record collector counts.
66
for (int i = 0; i < iterations; i++) {
68
addCollectionCount(counts, i);
71
// Check the increments:
72
// Old gen collectors should increase by one,
73
// New collectors may or may not increase.
74
// Any increase >=2 is unexpected.
75
for (String collector : counts.keySet()) {
76
System.out.println("Checking: " + collector);
78
for (int i = 0; i < iterations - 1; i++) {
79
List<Long> theseCounts = counts.get(collector);
80
long a = theseCounts.get(i);
81
long b = theseCounts.get(i + 1);
84
errorMessage += "Collector '" + collector + "' has increment " + (b - a) +
85
" at iteration " + i + "\n";
90
System.err.println(errorMessage);
91
throw new RuntimeException("FAILED: System.gc collections miscounted.");
93
System.out.println("Passed.");
96
private static void addCollectionCount(HashMap<String, List<Long>> counts, int iteration) {
97
for (int i = 0; i < collectors.size(); i++) {
98
GarbageCollectorMXBean collector = collectors.get(i);
99
List<Long> thisList = counts.get(collector.getName());
100
thisList.add(collector.getCollectionCount());