8193072: File.delete() should remove its path from DeleteOnExitHook.files
    Jason Mehrens 
    jason_mehrens at hotmail.com
       
    Mon Jul  8 20:53:59 UTC 2019
    
    
  
Brian,
Previously File.delete wouldn't throw IllegalStateException and with this patch it looks like that is possible (and not desirable).  I would think that this change could the break java.util.logging.FileHandler because Handler.close runs in a shutdown hook.
Jason
________________________________________
From: core-libs-dev <core-libs-dev-bounces at openjdk.java.net> on behalf of Brian Burkhalter <brian.burkhalter at oracle.com>
Sent: Monday, July 8, 2019 3:11 PM
To: core-libs-dev
Subject: 8193072: File.delete() should remove its path from DeleteOnExitHook.files
https://bugs.openjdk.java.net/browse/JDK-8193072 <https://bugs.openjdk.java.net/browse/JDK-8193072>
There does appear to be a memory leak of sorts if one does something like
—
File[] files;
for (int i = 0; i < largeNumber; i++) {
    files[i] = File.createTempFile(“blah”, null);
    files[i].deleteOnExit();
}
// do something
for (int i = 0; i < largeNumber; i++) {
    files[i].delete();
}
// do something else before shutdown
—
The LinkedHashSet in DeleteOnExitHook will contain at least largeNumber Files until the VM shuts down even though the files were deleted.
The potential change is included below. The additional call to DeleteOnExitHook.remove() in File.delete() does not appear to have a measurable performance impact, at least trivially and in isolation.
Thanks,
Brian
--- a/src/java.base/share/classes/java/io/DeleteOnExitHook.java
+++ b/src/java.base/share/classes/java/io/DeleteOnExitHook.java
@@ -64,6 +64,15 @@
         files.add(file);
     }
+    static synchronized void remove(String file) {
+        if(files == null) {
+            // DeleteOnExitHook is running. Too late to remove a file
+            throw new IllegalStateException("Shutdown in progress");
+        }
+
+        files.remove(file);
+    }
+
     static void runHooks() {
         LinkedHashSet<String> theFiles;
--- a/src/java.base/share/classes/java/io/File.java
+++ b/src/java.base/share/classes/java/io/File.java
@@ -1050,6 +1050,7 @@
         if (isInvalid()) {
             return false;
         }
+        DeleteOnExitHook.remove(path);
         return fs.delete(this);
     }
    
    
More information about the core-libs-dev
mailing list