Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Dispose() or Finalize() be used to delete temporary files?

Tags:

I have a class that makes use of temporary files (Path.GetTempFileName()) while it is active. I want to make sure these files do not remain on the user's hard drive taking up space after my program is closed. Right now my class has a Close() method which checks if any temporary files used by the class still exist and deletes them.

Would it make more sense to put this code in the Dispose() or Finalize() methods instead?

like image 657
Eric Anastas Avatar asked Jul 13 '10 19:07

Eric Anastas


People also ask

Which temporary files should be deleted?

If you're running low on storage space, you should consider deleting the temp files. You can either delete some or all of the temp files. Deleting them will free up space that you can use for other files and data. Keep in mind that you may not be able to delete temp files while the respective program is still running.

Does deleting temporary files do anything?

You should regularly delete temporary files to free up space, speed up your computer, and reduce the risk of errors, bugs, and crashes. When your drive gets too full, it can slow down — and you won't have any room to save new files. Low disk space makes your computer perform worse.

Is there anything important in temporary files?

There's nothing harmful about temporary files, but once the cached data they contain has served its purpose, they're no longer needed. Ideally, unneeded temp files should be deleted by the process or program that created them.


2 Answers

Better yet would be to create the file with FileOptions.DeleteOnClose. This will ensure that the operating system forcibly deletes the file when your process exits (even in the case of a rude abort). Of course, you will still want to close/delete the file yourself when you are done with it, but this provides a nice backstop to ensure that you don't allow the files to be sit around forever

Example:

using (FileStream fs = File.Create(Path.GetTempFileName(), Int16.MaxValue, 
       FileOptions.DeleteOnClose)) 
{ 

    // Use temp file 

} // The file will be deleted here
like image 118
star Avatar answered Sep 20 '22 13:09

star


I would do both; make the class disposable, and have the finalizer clean it up. There is a standard pattern for doing so safely and effectively: use it rather than attempting to deduce for yourself what the right pattern is. It is very easy to get wrong. Read this carefully:

http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Note that you've got to be really really careful when writing a finalizer. When the finalizer runs, many of your normal assumptions are wrong:

  • There are all kinds of potentials for race conditions or deadlocks because you are no longer on the main thread, you're on the finalizer thread.

  • In regular code, if you're running code inside an object then you know that all the things the object refers to are alive. In a finalizer, all the things the object refers to might have just been finalized! Finalizers of dead objects can run in any order, including "child" objects being finalized before "parent" objects.

  • In regular code, assigning a reference to an object to a static field could be perfectly sensible. In a finalizer, the reference you are assigning could be to an already dead object, and therefore the assignment brings a dead object back to life. (Because objects referred to by static fields are always alive.) That is an exceedingly weird state to be in and nothing pleasant happens if you do.

  • And so on. Be careful. You are expected to fully understand the operation of the garbage collector if you write a non-trivial finalizer.

like image 22
Eric Lippert Avatar answered Sep 19 '22 13:09

Eric Lippert