How to use garbage collection to delete files?

Hi, I use a lot of temporary files in java and my problem is that they are not deleted.

Without having to do my own temporary file management processing (it's not difficult I give you, but I'm a lazy plus with so many things to do, if I can save the reinvented wheel for this, the better) is there a way to provide temporary files to disk will be removed in a fairly normal way.

1 - using File tmp = File.createTempFile (), I can say tmp.deleteOnExit (), but if a thing works in the service, the only way to exit it is when it either crashes (rarely occurs) or when system crashes (for example, when the drive is completely full of temporary files and overthrows the cluster ... oops!)

Ideally, the created instances are collected at some point by the garbage collector, and since there is a lot of downtime in the application, it would just be a dandy if the GC could, well, finish cleaning it up and actually delete the file on disk, as well as dereferencing instance from memory.

The only way that I can see at the moment is to overload the File class and add a finalized method ... If I do this, I will also take care of my own temp file manager!

In short, can I use the garbage collector to clean up system resources (like files)?


Thank you all for your answers. I accepted Kristofffer because it was the easiest to implement, and that is exactly what I did.

I think that they cleaned me after so many years, making me forget about the basic household that I was, although I did the hard way in the days of C ++ goodwill.

+6
java garbage-collection temporary-files
source share
7 answers

Of course. The question is do you really want to :)

I really ran into this problem in the wild; as you noticed, clearing temporary files with deleteOnExit () is useless when starting the service, not the application. I found that the most sustainable solution was to restructure the program flow, so that temporary files were created based on each task and were explicitly deleted when they were no longer needed.

If you do this in any other way, that is, if the program cannot decide whether to store or delete the temporary file at any time during execution, there may be a design problem. Wrapping files in some manager teams simply postpones the "real" solution;)

+2
source share

You might want to explore the PhantomReference :

Phantom reference objects that are queued after the collector determines that their referents can be returned. Phantom links are most often used to plan attacks to prevent tampering in a more flexible way than is possible with the Java finalization mechanism.

+2
source share

Based on an event that needs to be triggered when your class is destroyed, it will not be reliable and may leave files behind.

I think the easiest and most reliable way to get your temporary files clean is as follows:

  • Write an event handler when your program closes to clear all the files that you opened during the current session.
  • Write a procedure that will be launched when your program starts, which will delete any temporary files in this folder older than 24 hours.

With this approach, you do not need to be afraid if for some reason your program crashes and leaves temporary files behind, and you also do not need to worry about the program deleting files that are still in use.

+2
source share

Garbage collection is the wrong namespace for this type of information processing.

The following paragraph should be sufficient to process temporary files.

  • You should try to delete the file immediately after you no longer use this file. The completion block can handle this.

  • Delete the OnExit that you should use.

  • You can create your temporary files in a special temp directory. This is a temporary directory that you can delete when the application is started and closed to make sure that temporary files do not exist after the application is closed.

+1
source share

The garbage collector is not useful for such things. It is designed to manage memory and can have many flaws.

  • Your object may be assembled for a long time after the file is no longer used.
  • Your object cannot be assembled at all.

Both things happen especially often if Java runs with a large heap size - a thing that is not unusual on the server side.

At some point in your program, you need to close the streams in this file (otherwise you will have system descriptor files, which makes the system unusable, I already did this). The moment you close the streams, you can also delete the linked file. This is the cleanest way.

+1
source share

Well, you can do a wrapper for a file that uses finalizer to delete your file when the object is garbage collected.

But finalizers are not called in any predictable way, so I really do not recommend this at all.

0
source share

The garbage collector is not the place to free this resource. See the next two articles on how to release Java resources at http://c2.com/cgi/wiki?ReleasingResourcesInJava , as well as performance issues with Java finalizers. They can provide some insight and understanding of how to use it. http://www.enyo.de/fw/notes/java-gc-finalizers.html

0
source share

All Articles