Skip to content

Instantly share code, notes, and snippets.

@maxdeliso
Last active December 15, 2015 14:09
Show Gist options
  • Save maxdeliso/5271948 to your computer and use it in GitHub Desktop.
Save maxdeliso/5271948 to your computer and use it in GitHub Desktop.
simple java garbage collector torture test
/*
* in this directory:
* to build:
* javac -Xlint:all GCTest.java
*
* to run:
* java -verbose:gc -server -Xincgc -Xmx1024m -classpath . GCTest
*
*/
import java.util.Random;
class GCTest {
final class GCTestParams {
static final int DEFAULT_MAX_BUCKET_SIZE = 512;
static final int DEFAULT_NUM_BUCKET_COUNT = 4096;
static final int DEFAULT_NUM_ITERATIONS = -1;
final int maxBucketSize;
final int maxBucketCount;
final int numIterations;
public GCTestParams() {
this(
DEFAULT_MAX_BUCKET_SIZE,
DEFAULT_NUM_BUCKET_COUNT,
DEFAULT_NUM_ITERATIONS);
}
public GCTestParams(int _maxBucketSize) {
this(
_maxBucketSize,
DEFAULT_NUM_BUCKET_COUNT,
DEFAULT_NUM_ITERATIONS);
}
public GCTestParams(
int _maxBucketSize,
int _maxBucketCount) {
this(
_maxBucketSize,
_maxBucketCount,
DEFAULT_NUM_ITERATIONS);
}
public GCTestParams(
int _maxBucketSize,
int _maxBucketCount,
int _numIterations) {
maxBucketSize = _maxBucketSize;
maxBucketCount = _maxBucketCount;
numIterations = _numIterations;
}
public String toString() {
return
"max size: " + maxBucketSize + "\n" +
"max count: " + maxBucketCount + "\n" +
"max iterations: " + numIterations;
}
}
final GCTestParams params;
public GCTest(String args[]) {
params = parseArgs(args);
}
public GCTestParams parseArgs(String args[]) {
try {
switch(args.length) {
case 3:
return new GCTestParams(
Integer.parseInt(args[0]),
Integer.parseInt(args[1]),
Integer.parseInt(args[2]));
case 2:
return new GCTestParams(
Integer.parseInt(args[0]),
Integer.parseInt(args[1]));
case 1:
return new GCTestParams(Integer.parseInt(args[0]));
case 0:
return new GCTestParams();
default:
System.err.println("invalid number of args, [0-3]");
System.exit(1);
return null;
}
} catch( NumberFormatException nfe ) {
System.err.println("invalid arg format, [ints required]");
System.exit(1);
return null;
}
}
public void runTest() {
Random LCG = new Random();
Runtime runtime = Runtime.getRuntime();
byte [][] buckets = new byte[params.maxBucketCount][];
System.out.println( "starting with params: " + params);
for(int i = 0;
params.numIterations == -1 || i < params.numIterations;
++i) {
final int selectedBucket = LCG.nextInt(params.maxBucketCount);
final int requestedBytes = LCG.nextInt(params.maxBucketSize)+1;
System.out.println(
"r in b[" + selectedBucket +
"] for " + requestedBytes);
buckets[selectedBucket ] = new byte[requestedBytes];
System.out.println(
" heap is now " +
((float)runtime.freeMemory()/runtime.maxMemory()*100.0) + "% full");
}
}
public static void main(String args[]) {
GCTest gct = new GCTest(args);
gct.runTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment