Skip to content

Instantly share code, notes, and snippets.

@kinshuk4
Created June 25, 2015 13:38
Show Gist options
  • Save kinshuk4/f3119a408b56ce33899c to your computer and use it in GitHub Desktop.
Save kinshuk4/f3119a408b56ce33899c to your computer and use it in GitHub Desktop.
package com.vaani.java.gc;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MappedFileGc {
public static void main(String[] args) throws IOException {
File tempFile = File.createTempFile("Temp", null);
tempFile.deleteOnExit();
RandomAccessFile raTempFile = new RandomAccessFile(tempFile, "rw");
FileChannel fChannel = raTempFile.getChannel();
MappedByteBuffer mappedBuffer = fChannel.map(
FileChannel.MapMode.READ_WRITE, 0, 512);
fChannel.close();
raTempFile.close();
mappedBuffer = null;
System.gc();
if (tempFile.delete())
System.out.println("Successfully deleted: " + tempFile);
else
System.out.println("Unable to delete: " + tempFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment