86 }
87 }
88
89 class SystemGCTask extends Exitable implements Runnable {
90 private long delayMS;
91
92 SystemGCTask(long delayMS) {
93 this.delayMS = delayMS;
94 }
95
96 @Override
97 public void run() {
98 while (!shouldExit()) {
99 System.gc();
100 ThreadUtils.sleep(delayMS);
101 }
102 }
103 }
104
105 public class TestSystemGC {
106 private static final int numGroups = 7;
107 private static final int numGCsPerGroup = 4;
108
109 private static Map<String, String> longLivedMap = new TreeMap<>();
110
111 private static void populateLongLived() {
112 for (int i = 0; i < 1000000; i++) {
113 String key = "all" + " key = " + i;
114 String value = "the value is " + i;
115 longLivedMap.put(key, value);
116 }
117 }
118
119 private static long getDelayMS(int group) {
120 if (group == 0) {
121 return 0;
122 }
123
124 int res = 16;
125 for (int i = 0; i < group; i++) {
126 res *= 2;
127 }
128 return res;
129 }
130
131 private static void doSystemGCs() {
132 ThreadUtils.sleep(1000);
133
134 for (int i = 0; i < numGroups; i++) {
135 for (int j = 0; j < numGCsPerGroup; j++) {
136 System.gc();
137 ThreadUtils.sleep(getDelayMS(i));
138 }
139 }
140 }
141
142 private static SystemGCTask createSystemGCTask(int group) {
143 long delay0 = getDelayMS(group);
144 long delay1 = getDelayMS(group + 1);
145 long delay = delay0 + (delay1 - delay0) / 2;
146 return new SystemGCTask(delay);
147 }
148
149 private static void startTask(Runnable task) {
150 if (task != null) {
151 new Thread(task).start();
152 }
153 }
154
155 private static void exitTask(Exitable task) {
156 if (task != null) {
157 task.exit();
158 }
159 }
160
161 private static void runAllPhases() {
162 for (int i = 0; i < 4; i++) {
163 SystemGCTask gcTask =
164 (i % 2 == 1) ? createSystemGCTask(numGroups / 3) : null;
165 ShortLivedAllocationTask shortTask =
166 (i == 1 || i == 3) ? new ShortLivedAllocationTask() : null;
167 LongLivedAllocationTask longTask =
168 (i == 2 || i == 3) ? new LongLivedAllocationTask(longLivedMap) : null;
169
170 startTask(gcTask);
171 startTask(shortTask);
172 startTask(longTask);
173
174 doSystemGCs();
175
176 exitTask(gcTask);
177 exitTask(shortTask);
178 exitTask(longTask);
179
180 ThreadUtils.sleep(1000);
181 }
182 }
183
184 public static void main(String[] args) {
185 // First allocate the long lived objects and then run all phases.
186 populateLongLived();
187 runAllPhases();
188 if (args.length > 0 && args[0].equals("long")) {
189 runAllPhases();
190 }
191 }
192 }
|
86 }
87 }
88
89 class SystemGCTask extends Exitable implements Runnable {
90 private long delayMS;
91
92 SystemGCTask(long delayMS) {
93 this.delayMS = delayMS;
94 }
95
96 @Override
97 public void run() {
98 while (!shouldExit()) {
99 System.gc();
100 ThreadUtils.sleep(delayMS);
101 }
102 }
103 }
104
105 public class TestSystemGC {
106 private static long endTime;
107
108 private static final int numGroups = 7;
109 private static final int numGCsPerGroup = 4;
110
111 private static Map<String, String> longLivedMap = new TreeMap<>();
112
113 private static void populateLongLived() {
114 for (int i = 0; i < 1000000; i++) {
115 String key = "all" + " key = " + i;
116 String value = "the value is " + i;
117 longLivedMap.put(key, value);
118 }
119 }
120
121 private static long getDelayMS(int group) {
122 if (group == 0) {
123 return 0;
124 }
125
126 int res = 16;
127 for (int i = 0; i < group; i++) {
128 res *= 2;
129 }
130 return res;
131 }
132
133 private static void doSystemGCs() {
134 ThreadUtils.sleep(1000);
135
136 for (int i = 0; i < numGroups; i++) {
137 for (int j = 0; j < numGCsPerGroup; j++) {
138 System.gc();
139 if (System.currentTimeMillis() >= endTime) {
140 return;
141 }
142 ThreadUtils.sleep(getDelayMS(i));
143 }
144 }
145 }
146
147 private static SystemGCTask createSystemGCTask(int group) {
148 long delay0 = getDelayMS(group);
149 long delay1 = getDelayMS(group + 1);
150 long delay = delay0 + (delay1 - delay0) / 2;
151 return new SystemGCTask(delay);
152 }
153
154 private static void startTask(Runnable task) {
155 if (task != null) {
156 new Thread(task).start();
157 }
158 }
159
160 private static void exitTask(Exitable task) {
161 if (task != null) {
162 task.exit();
163 }
164 }
165
166 private static void runAllPhases() {
167 for (int i = 0; i < 4 && System.currentTimeMillis() < endTime; i++) {
168 SystemGCTask gcTask =
169 (i % 2 == 1) ? createSystemGCTask(numGroups / 3) : null;
170 ShortLivedAllocationTask shortTask =
171 (i == 1 || i == 3) ? new ShortLivedAllocationTask() : null;
172 LongLivedAllocationTask longTask =
173 (i == 2 || i == 3) ? new LongLivedAllocationTask(longLivedMap) : null;
174
175 startTask(gcTask);
176 startTask(shortTask);
177 startTask(longTask);
178
179 doSystemGCs();
180
181 exitTask(gcTask);
182 exitTask(shortTask);
183 exitTask(longTask);
184
185 ThreadUtils.sleep(1000);
186 }
187 }
188
189 public static void main(String[] args) throws Exception {
190 if (args.length == 0) {
191 throw new IllegalArgumentException("Must specify timeout in seconds as first argument");
192 }
193 int timeout = Integer.parseInt(args[0]) * 1000;
194 System.out.println("Running with timeout of " + timeout + "ms");
195 endTime = System.currentTimeMillis() + timeout;
196 // First allocate the long lived objects and then run all phases.
197 populateLongLived();
198 runAllPhases();
199 }
200 }
|