java.io.File.createTempFile().deleteOnExit() for that File object. This code is used in many scenarios in my application. Sometimes, the size of temp files is too large and so I have to delete it immediately after my job is completed. So I am calling File.delete() for some objects.
Now the problem is, when I delete the file using delete() function, the reference pointer is open for this deleted file(because of it being temp file(My opinion)). Because of this, I am facing memory leakage issue.
(Correct me if I am wrong on my above hypothesis)
I am facing high disk utilization on my environment, I found a discrepancy of over 30GB in the output of the 'df' and 'du' command ('df' looks at the stat of the FS itself whereas 'du' ignores deleted file descriptors).
lsof +al1 on linux to see open files) Why is this happening?Is there any Solution on how I can remove file from deleteOnExit() list if I am manually deleting the file?
I suspect that your analysis is correct, and that could be seen as a bug in Java: once you call delete, it would be fair to expect the reference created by deleteOnExit to be be removed.
However, we are at least warned (sort of). The Javadoc for deleteOnExit says:
Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.
So I guess calling delete after deleteOnExit would be considered careless.
However, it seems to me that your question implies its own solution. You say:
If I remove delete(), then I will have to wait until VM stops to get tempFiles deleted(which is a very rare case in Production Server).
If the JVM is very rarely ended, then deleteOnExit will very rarely do you any good. Which suggests that the solution is to handle your own deletions, by having your application call delete when it is finished with a file, and note use deleteOnExit at all.
The pointer will remain open until the application releases the resource for that file. Try
fileVar = null;
after your
fileVar.delete();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With