The deleteOnExit () function of the file keeps the pointer reference open even after the file is deleted

  • I am creating temporary files in my application using java.io.File.createTempFile()

    .
  • When creating the file, I called deleteOnExit()

    File.

This code is used in many scenarios in my application. Sometimes the size of the temporary files is too large, so I have to delete it immediately after my work completes. So I am calling File.delete()

for some objects.

Now the problem is that when I delete a file using a function delete()

, the reference pointer is open for that deleted file (due to the fact that it is a temporary file (My opinion)). Because of this, I am facing a memory leak problem.

(Correct me if I am wrong in my hypothesis above)

I ran into high disk usage in my environment, I found a mismatch of more than 30GB in the output of the "df" and "du" command ("df" looks at the statistics of the FS itself, whereas "du" ignores deleted file descriptors).

  • If I delete deleteOnExit (), I have to take care of manually deleting all objects. By doing this, my pointers are still open (used lsof +al1

    in Linux to view open files). Why is this happening?
  • If I delete delete () I have to wait for the VM to stop deleting tempFiles (which is very rare in Production Server). (Use of huge space)

Is there a solution on how to remove a file from the deleteOnExit () list if I manually delete the file?

+3


source to share


3 answers


I suspect your parsing is correct, and this can be viewed as a bug in Java: as soon as you call delete

, it would be fair to expect that the link created deleteOnExit

will be removed.

However, we are at least warning (sort of). The javadoc fordeleteOnExit

says:

Once a deletion has been requested, you cannot cancel the request. Therefore, this method should be used with caution.

So my guess is that calling delete

after deleteOnExit

would be considered careless.



However, it seems to me that your question implies its own solution. You speak:

If I delete delete () I have to wait for the VM to stop deleting tempFiles (which is very rare on a Production Server).

If the JVM very rarely ends up then it deleteOnExit

will very rarely do you well. This suggests that the solution is to handle your own deletions, by calling your application delete

when finished with the file, and generally using deleteOnExit

.

+1


source


The pointer will remain open until the application releases the resource for this file. Try

fileVar = null;

      



after

fileVar.delete();

      

0


source


I see this problem too. I temporarily "fixed" it by doing the following, before calling delete:

FileChannel outChan = new FileOutputStream(tmpfile, true).getChannel();
outChan.truncate(newSize);
outChan.close();

      

This at least makes it so that tmp files do not take up disk space, and df and du report the same statistics. It still leaks file descriptors, and I'm guessing it leaks a small amount of the heap.

It should be noted that File.delete () returns a boolean indicating whether the delete succeeded. It is possible that this is silent to you and you actually have an unclosed stream to the file that prevents deletion. You can try the following call, which will throw a diagnostic IOException if it fails to delete the file.

java.nio.file.Files.delete(tmpfile.toPath)

      

If that doesn't isolate the problem for you yet, I was fortunate enough to use a file-leak-detector , which monitors timing files accessed through streams and captures a stack trace during stream creation. If the stream is not closed, the stack trace can point to the source of that stream. Unfortunately, it doesn't cover all forms of file access like nio.

0


source







All Articles