Java File deleteOnExit memory leak
Monday, December 31st, 2007Well I would never have ever expected to find such a strange memory leak in my program.
The program ran for a long time and called the following code
final File tempfile = File.createTempFile("tmp", ".dat"); tempfile.deleteOnExit();
I then deleted the file in a normal case using
tempfile.delete()
You would never think this would leak memory but it does. I simply called deleteOnExit() to ensure anything I missed was cleaned up. The program didnt rely on it.
The reason that information is kept in memory every time the deleteOnExit call is made, this used to be in the native code, but its now in the java code, is so the files can be deleted. In the java code the absolute path is stored in a HashMap. When a file is deleted the entry still remains in this HashMap.
Simply put, dont use deleteOnExit() if your program should run for a long time.
See http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513817
It also appears other people are seeing this issue too http://www.jroller.com/javabean/entry/solving_an_outofmemoryerror_java_6