JDK-8193072 : File.delete() should remove its path from DeleteOnExitHook.files
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 10
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2017-12-05
  • Updated: 2019-11-06
Related Reports
Duplicate :  
Relates :  
When calling File.deleteOnExit() it adds a path to the static list DeleteOnExitHook.files so that it can be deleted when the vm terminates. However there is no need for that when the file was deleted previously by File.delete().

Moreover a careful developer might think to create a temporary file which she deletes later within her application by File.delete(), but just in case something goes wrong in between she also calls File.deleteOnExit() after creation. Now if that application would create an infinite amount of such temporary files, it would create a subtile memory leak, as File.deleteOnExit() grows an unbounded list which is unaffected by File.delete().

I ask to add the said feature to avoid this subtle opportunity to create a memory leak in case of an application which needs to create endless temporary files and deletes them by itself.

The file may be deleted and re-created many times so having File::delete remove it from the deleteOnExit list isn't right. Applications using temporary work files may be able to use the DELETE_ON_CLOSE option as it avoids the need to use the deleteOnExit mechanism.