Archive for December, 2007

Java File deleteOnExit memory leak

Monday, December 31st, 2007

Well 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");


I then deleted the file in a normal case using


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.


It also appears other people are seeing this issue too