Skip to content

Instantly share code, notes, and snippets.

@paladin8
Last active December 19, 2015 23:38
public class GCTest {
public void handle1() {
byte[] bytes = getBytes();
handleBytes(bytes);
}
public void handle2() {
int[] ints = getInts(getBytes());
handleInts(ints);
}
private byte[] getBytes() {
return new byte[256 * 1024 * 1024];
}
private int[] getInts(byte[] bytes) {
int[] ints = new int[bytes.length / 4];
for (int i = 0; i < ints.length; i+=4) {
ints[i] = (bytes[4*i]<<24) + (bytes[4*i+1]<<16) +
(bytes[4*i+2]<<8) + bytes[4*i+3];
}
return ints;
}
private void handleBytes(byte[] bytes) {
handleInts(getInts(bytes));
}
private void handleInts(int[] ints) {
List<byte[]> allocated = new ArrayList<byte[]>();
while (true) {
try {
allocated.add(new byte[1024 * 1024]);
} catch(Throwable e) {
System.out.println(allocated.size() + " " + ints.length);
System.exit(0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment