Skip to content

Instantly share code, notes, and snippets.

@raananraz
Created April 29, 2015 17:03
Show Gist options
  • Save raananraz/5c466d6208a0f1429f20 to your computer and use it in GitHub Desktop.
Save raananraz/5c466d6208a0f1429f20 to your computer and use it in GitHub Desktop.
FSTObjectInput - OutOfMemoryError
public static void main(String[] args) throws Exception {
File temp = File.createTempFile("test", "dat");
final int BUFFER_SIZE_IN_BYTES = 10 * 1024 * 1024;
final int MAX_ITEMS_BEFORE_FLUSH = 10000;
final int NUMBER_OF_ITEMS = 1000000;
try {
FSTConfiguration config = FSTConfiguration.getDefaultConfiguration();
config.setPreferSpeed(true);
config.setShareReferences(false);
int numberOfObjects = 0;
try (FileOutputStream fileOutputStream = new FileOutputStream(temp)) {
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE_IN_BYTES)) {
try (FSTObjectOutput fstObjectOutput = new FSTObjectOutput(bufferedOutputStream, config)) {
for (int i = 0; i < NUMBER_OF_ITEMS; i++) {
Object[] arr = new Object[100];
for (int objIdx = 0; objIdx < arr.length; objIdx++) {
arr[objIdx] = "row " + i + " - " + "my object" + objIdx;
}
fstObjectOutput.writeObject(arr);
numberOfObjects++;
if (i % MAX_ITEMS_BEFORE_FLUSH == 0) {
System.out.println("writing " + i);
fstObjectOutput.flush();
}
}
}
}
}
System.out.println("done with write");
try (FileInputStream fileInputStream = new FileInputStream(temp)) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream, BUFFER_SIZE_IN_BYTES)) {
try (FSTObjectInput fstObjectInput = new FSTObjectInput(bufferedInputStream, config)) {
for (int idx = 0; idx < numberOfObjects; idx++) {
Object[] row = (Object[]) fstObjectInput.readObject();
if (idx % MAX_ITEMS_BEFORE_FLUSH == 0) {
fstObjectInput.reset();
config.clearCaches();
System.out.println("reading " + idx);
}
}
}
}
}
System.out.println("done with read");
}
finally {
temp.delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment