Skip to content

Instantly share code, notes, and snippets.

@wangzaixiang
Created August 30, 2012 09:22
Show Gist options
  • Save wangzaixiang/3524739 to your computer and use it in GitHub Desktop.
Save wangzaixiang/3524739 to your computer and use it in GitHub Desktop.
GC Test Program
package demo;
import java.util.ArrayList;
import java.util.List;
public class KillTheGarbageCollector {
private List<String> outerStore = new ArrayList<>(1_000_000);
private List<String> oldStore = new ArrayList<>(1_000_000);
private static int nThread = 3;
public static void main(String[] args) {
for (int i = 0; i < nThread; ++i) {
new Thread() {
public void run() {
long start = System.currentTimeMillis();
new KillTheGarbageCollector().go();
long end = System.currentTimeMillis();
System.out.println(Thread.currentThread() + " runs for " + (end-start) + "ms");
}
}.start();
}
}
private void go() {
long time = System.currentTimeMillis();
for (int i = 0; i < 100; ++i) {
if (i % 10 == 0) {
doOld();
time = System.currentTimeMillis(); // reset the clock
}
for (int j = 0; j < 1000000 / nThread; ++j) {
outerStore.add(((Integer) j).toString());
}
outerStore.clear();
long t = System.currentTimeMillis();
//System.out.println("" + (t - time));
time = t;
}
}
private void doOld() {
oldStore.clear();
for (int j = 0; j < 5_000_000 / nThread; ++j) {
oldStore.add(((Integer) j).toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment