-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterleaveKlassRefsInHeap.java
151 lines (113 loc) · 5.03 KB
/
InterleaveKlassRefsInHeap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package de.stuefe.repros.metaspace;
import de.stuefe.repros.MiscUtils;
import de.stuefe.repros.TestCaseBase;
import de.stuefe.repros.metaspace.internals.InMemoryClassLoader;
import de.stuefe.repros.metaspace.internals.Utils;
import picocli.CommandLine;
import java.lang.reflect.Constructor;
import java.util.Random;
import java.util.concurrent.Callable;
@CommandLine.Command(name = "ManyClassesManyObjectsFullGC", mixinStandardHelpOptions = true,
description = "ManyClassesManyObjectsFullGC repro.")
public class InterleaveKlassRefsInHeap extends TestCaseBase implements Callable<Integer> {
@CommandLine.Option(names = { "--autoyes", "-y" }, defaultValue = "false",
description = "Autoyes.")
boolean auto_yes;
int unattendedModeWaitSecs = 4;
@CommandLine.Option(names = { "--nowait" }, defaultValue = "false",
description = "do not wait (only with autoyes).")
boolean nowait;
@CommandLine.Option(names = { "--verbose", "-v" }, defaultValue = "false",
description = "Verbose.")
boolean verbose;
public static void main(String... args) {
int exitCode = new CommandLine(new InterleaveKlassRefsInHeap()).execute(args);
System.exit(exitCode);
}
@CommandLine.Option(names = { "--num-classes", "-C" }, description = "Number of classes (default: ${DEFAULT_VALUE})")
int numClasses=256;
@CommandLine.Option(names = { "--objects", "-n" }, description = "Number of objects per class (default: ${DEFAULT_VALUE})")
int numObjectsPerClass=50000;
@CommandLine.Option(names = { "--cycles", "-c" }, description = "Number of GC cycles (default: ${DEFAULT_VALUE})")
int cycles = 10;
@CommandLine.Option(names = { "--print-hashes" }, description = "Print hashes (default: ${DEFAULT_VALUE})")
boolean printHashes = false;
@CommandLine.Option(names = { "--randomize" }, description = "Randomizes object classes (off: strict interleaving) (default: ${DEFAULT_VALUE})")
boolean randomize = false;
@CommandLine.Option(names = { "--class-stride" }, description = "If not --randomize: stride by which we alter the class per object (default: ${DEFAULT_VALUE})")
int classStride = 1;
static Object[] RETAIN;
@Override
public Integer call() throws Exception {
initialize(verbose, auto_yes, nowait);
System.out.println("Num Classes: " + numClasses);
System.out.println("Num Objects per Class: " + numObjectsPerClass);
System.out.println("Cycles: " + cycles);
System.out.println("Class association: " + (randomize ? "random" : "striding with " + classStride));
Random r = new Random(0x12345678);
final String source = "" +
"public class CLASSNAME {" +
"}";
System.out.print("Generate " + numClasses + " classes...");
for (int i = 0; i < numClasses; i ++) {
String classname = "my_generated_class" + i;
String source0 = source.replace("CLASSNAME", classname);
Utils.createClassFromSource(classname, source0);
}
System.out.println();
System.out.print("Loading " + numClasses + "...");
InMemoryClassLoader loader = new InMemoryClassLoader("loader", null);
Class classes[] = new Class[numClasses];
try {
for (int j = 0; j < numClasses; j++) {
classes[j] = Class.forName("my_generated_class" + j, true, loader);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.gc();
System.out.print("Creating " + numObjectsPerClass + " objects per class...");
RETAIN = new Object[numObjectsPerClass * numClasses];
int idx = 0;
Constructor ctors[] = new Constructor[numClasses];
for (int i = 0; i < numClasses; i++) {
Class clazz = classes[i];
Constructor ctor = clazz.getDeclaredConstructor();
ctors[i] = ctor;
}
// Create objects in heap
for (int j = 0; j < numObjectsPerClass; j++) {
for (int i = 0; i < numClasses; i++) {
int classId = randomize ?
r.nextInt(numClasses) :
(i * classStride) % numClasses;
RETAIN[idx] = ctors[classId].newInstance();
idx++;
}
}
System.out.println();
if (printHashes) {
int col = 0;
for (Object o: RETAIN) {
int i = System.identityHashCode(o);
System.out.print(Integer.toHexString(i) + " ");
if (++col == 16) {
col = 0;
System.out.println("");
}
}
}
System.out.println("Done; will start GCs... ");
if (!nowait) {
MiscUtils.waitForKeyPress();
}
for (int i = 0; i < cycles; i++) {
System.gc();
}
System.out.println("After GCs... ");
if (!nowait) {
MiscUtils.waitForKeyPress();
}
return 0;
}
}