title: File cannot be deleted because the JVM holds it - a tricky one
body: <p>My post got a little too long, sorry. Here is a summary:</p>
  
  <ul>
  <li>File on disk cannot be deleted ("the JVM holds the file" error). both when deleting from the java code and when trying to manually delete the file from windows.</li>
  <li>All streams to that file are <em>closed</em> and set to null. All file objects set to null.</li>
  <li>The program does nothing at that point; but waiting 30 minutes allows me to deleted the file from windows. Weird. Is the file not used by java anymore? Plus, since nothing happens in the program, it indicates it cannot be some stream I forgot (plus, I triple checked nothing is open).</li>
  <li>Invoking System.gc() seemed to work when files were small. Did not help when they got to about 20MB.</li>
  </ul>
  
  <hr>
  
  <p>[EDIT2] - I tried writing some basic code to explain, but its tricky. I am sorry, I know it's difficult to answer like that. I can however write how I open and close streams, of course:</p>
  
  <pre><code>BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:\\folder\\myFile.txt")));
  for(int i = 0; i &lt; 10; i++)
  {
      bw.write("line " + i);
      bw.newLine();
  }
  bw.close();
  bw = null;
  </code></pre>
  
  <p>If I've used a file object:</p>
  
  <pre><code>File f = new File("C:\\folder\\myFile.txt");
  // use it...
  f = null;
  </code></pre>
  
  <p>Basic code, I know. But this is essentially what I do.
  I know <em>for a fact</em> I've closed all streams in this exact way. 
  I know <em>for a fact</em> that nothing happens in the program in that 30-minutes interval in which I cannot delete the file, until I somehow magically can.</p>
  
  <p>thank you for your input even without the coherent code.
  I appreciate that.</p>
  
  <hr>
  
  <p>Sorry for not providing any specific code here, since I can't pinpoint the problem (not exactly specific-code related). In any case, here is the thing:</p>
  
  <p>I have written a program which reads, writes and modifies files on disk. For several reasons, the handling of the read/write is done in a different thread, which is constantly operating. </p>
  
  <p>At some point, I am terminating the "read/write" thread, keeping only the main thread - it waits for input from a socket, totally unrelated to the file, and does nothing. Then, I try to delete the file (using either File.delete(), even tried nio.Files delete option).</p>
  
  <p>The thing is - and it's very weird - sometimes it works, sometimes it doesn't. Even manually, going to the folder and trying to delete the file via windows, gives me the "The file is open by the JVM" message.</p>
  
  <p>Now, I am well aware that keeping references from all kinds of streams to the file prevents me from deleting it. Well past that by now :)
  I have made sure that all streams are closed. I even set their values to null, including any "File" objects I have used (even though it shouldn't make any difference). All set to null, all closed. And the thread which generates all of them - the "read/write" thread - well, it's terminated since it got the the end of its run() method.</p>
  
  <p>Usually, if I wait about 30 minutes, <em>while the JVM still operates</em>, I can delete the file manually from windows. The error magically disappears. When the JVM is closed, I can <em>always</em> delete the file right away.</p>
  
  <p>I am lost here. Tried specifically invoking System.gc() before trying to delete the file, even called it like 10 times (not that it should matter). Sometimes it helped, but on other occasions, for example, when the file got larger (say 20MB), that didn't help.</p>
  
  <p>What am I missing here?
  Obviously, this couldn't be my implicit fault (not closing some stream), since the read/write thread is dead, the main thread awaits something unrelated (so the program is at a "standstill"), I have explicitly closed all streams, even nullified the references (inStream = null), invoked the garbage collector.</p>
  
  <p>What am I missing? Why is the file "deletable" after 30 minutes (nothing happens at that time - not something in my code). Am I missing some gentle reference/garbage collection thingy?</p>