Skip to content

Instantly share code, notes, and snippets.

@benstopford
Created March 18, 2014 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benstopford/9619299 to your computer and use it in GitHub Desktop.
Save benstopford/9619299 to your computer and use it in GitHub Desktop.
Small class to test GC performance
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Benjamin Stopford - 11/09/13
*/
public class GCInvestigation {
public static void main(String[] args) throws InterruptedException {
new GCInvestigation();
}
public GCInvestigation() throws InterruptedException {
long start = System.currentTimeMillis();
Map map = new HashMap();
int size = 3500000;
int count = 0;
while (count++ <3) {
try {
//allocate 1GB of heap;
for (int i = 0; i < size; i++) {
map.put(i, new byte[1024]);
}
System.out.format("Created 1 %,dMB of garbage.\n", map.size());
System.out.println("Should have failed");
Thread.sleep(1000);
} finally {
System.out.println("size of map " + map.size());
}
System.out.println("Took "+(System.currentTimeMillis() - start));
printGcinfo();
map.clear();
}
}
private void printGcinfo(){
List<GarbageCollectorMXBean> gcList = ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean tmpGC : gcList){
System.out.println(tmpGC.getName() + " time "+ tmpGC.getCollectionTime());
}
}
/**
-Xmx5g -Xms5g -XX:NewSize=4g -XX:ParallelGCThreads=12
Took 19849
PS Scavenge time 7938
PS MarkSweep time 5987
-Xmx5g -Xms5g -XX:NewSize=3g :
Took 12032
PS Scavenge time 641
PS MarkSweep time 5600
-Xmx5g -Xms5g -XX:NewSize=3g -XX:ParallelGCThreads=12
Took 12264
PS Scavenge time 752
PS MarkSweep time 5795
-Xmx5g -Xms5g -XX:NewSize=1g :
Took 14560
PS Scavenge time 2544
PS MarkSweep time 6463
-Xmx5g -Xms5g -XX:NewSize=1g -XX:ParallelGCThreads=12
Took 14527
PS Scavenge time 2450
PS MarkSweep time 6545
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment