34 */
35
36 import java.lang.management.GarbageCollectorMXBean;
37 import java.lang.management.ManagementFactory;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41
42 /*
43 * Originally for a specific failure in CMS, this test now monitors all
44 * collectors for double-counting of collections.
45 */
46 public class TestFullGCCount {
47
48 static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
49
50 public static void main(String[] args) {
51 int iterations = 20;
52 boolean failed = false;
53 String errorMessage = "";
54 HashMap<String, List> counts = new HashMap<>();
55
56 // Prime the collection of count lists for all collectors.
57 for (int i = 0; i < collectors.size(); i++) {
58 GarbageCollectorMXBean collector = collectors.get(i);
59 counts.put(collector.getName(), new ArrayList<>(iterations));
60 }
61
62 // Perform some gc, record collector counts.
63 for (int i = 0; i < iterations; i++) {
64 System.gc();
65 addCollectionCount(counts, i);
66 }
67
68 // Check the increments:
69 // Old gen collectors should increase by one,
70 // New collectors may or may not increase.
71 // Any increase >=2 is unexpected.
72 for (String collector : counts.keySet()) {
73 System.out.println("Checking: " + collector);
74
75 for (int i = 0; i < iterations - 1; i++) {
76 List<Long> theseCounts = counts.get(collector);
77 long a = theseCounts.get(i);
78 long b = theseCounts.get(i + 1);
79 if (b - a >= 2) {
80 failed = true;
81 errorMessage += "Collector '" + collector + "' has increment " + (b - a) +
82 " at iteration " + i + "\n";
83 }
84 }
85 }
86 if (failed) {
87 System.err.println(errorMessage);
88 throw new RuntimeException("FAILED: System.gc collections miscounted.");
89 }
90 System.out.println("Passed.");
91 }
92
93 private static void addCollectionCount(HashMap<String, List> counts, int iteration) {
94 for (int i = 0; i < collectors.size(); i++) {
95 GarbageCollectorMXBean collector = collectors.get(i);
96 List thisList = counts.get(collector.getName());
97 thisList.add(collector.getCollectionCount());
98 }
99 }
100 }
|
34 */
35
36 import java.lang.management.GarbageCollectorMXBean;
37 import java.lang.management.ManagementFactory;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41
42 /*
43 * Originally for a specific failure in CMS, this test now monitors all
44 * collectors for double-counting of collections.
45 */
46 public class TestFullGCCount {
47
48 static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
49
50 public static void main(String[] args) {
51 int iterations = 20;
52 boolean failed = false;
53 String errorMessage = "";
54 HashMap<String, List<Long>> counts = new HashMap<>();
55
56 // Prime the collection of count lists for all collectors.
57 for (int i = 0; i < collectors.size(); i++) {
58 GarbageCollectorMXBean collector = collectors.get(i);
59 counts.put(collector.getName(), new ArrayList<>(iterations));
60 }
61
62 // Perform some gc, record collector counts.
63 for (int i = 0; i < iterations; i++) {
64 System.gc();
65 addCollectionCount(counts, i);
66 }
67
68 // Check the increments:
69 // Old gen collectors should increase by one,
70 // New collectors may or may not increase.
71 // Any increase >=2 is unexpected.
72 for (String collector : counts.keySet()) {
73 System.out.println("Checking: " + collector);
74
75 for (int i = 0; i < iterations - 1; i++) {
76 List<Long> theseCounts = counts.get(collector);
77 long a = theseCounts.get(i);
78 long b = theseCounts.get(i + 1);
79 if (b - a >= 2) {
80 failed = true;
81 errorMessage += "Collector '" + collector + "' has increment " + (b - a) +
82 " at iteration " + i + "\n";
83 }
84 }
85 }
86 if (failed) {
87 System.err.println(errorMessage);
88 throw new RuntimeException("FAILED: System.gc collections miscounted.");
89 }
90 System.out.println("Passed.");
91 }
92
93 private static void addCollectionCount(HashMap<String, List<Long>> counts, int iteration) {
94 for (int i = 0; i < collectors.size(); i++) {
95 GarbageCollectorMXBean collector = collectors.get(i);
96 List<Long> thisList = counts.get(collector.getName());
97 thisList.add(collector.getCollectionCount());
98 }
99 }
100 }
|