Skip to content

Instantly share code, notes, and snippets.

@lukhnos
Last active September 4, 2015 15:34
Show Gist options
  • Save lukhnos/374f7b342e0a65a4cdfc to your computer and use it in GitHub Desktop.
Save lukhnos/374f7b342e0a65a4cdfc to your computer and use it in GitHub Desktop.
Demonstrates that FileChannelImpl and FileChannel leak memory in j2objc, see https://github.com/google/j2objc/issues/603
import com.google.j2objc.annotations.AutoreleasePool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class NIOLeaks {
public static void main(String args[]) {
for (int i = 0; ; i++) {
foo(i);
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
@AutoreleasePool
public static void foo(int i) {
try {
File f = File.createTempFile("test", "tmp");
System.out.println("Using temp: " + f);
FileOutputStream fos = new FileOutputStream(f);
FileChannel channel = fos.getChannel();
FileLock lock = channel.lock();
lock.close();
channel.close();
fos.close();
f.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment