Created
June 25, 2015 13:38
-
-
Save kinshuk4/f3119a408b56ce33899c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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