Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use garbage collection to delete files?

Hi I am using a lot of temporary files in java and my problem is that they do not get deleted.

Without having to implement my own handling of temporary file management (not hard I grant you but I am lazy plus with so many things to do If I can save reinventing the wheel for this all the better) is there a way to ensure the temp files on disk will get deleted in a fairly regular fashion.

1 - using File tmp = File.createTempFile(), sure I can say tmp.deleteOnExit() but If the thing runs in a service the only way it exits is when it either crashes (happens rarely), or when the system crashes (like when the drive is completely full of temp files and topples the cluster... oops !)

Ideally, the instances created gets collected at some point by the garbage collector, and since there is a LOT of idle time in the application it would just be dandy if the GC could, well, finish it`s cleanup and actually delete the file on disk as well when dereferencing the instance from memory.

the only way I see for now is overload the File class and add a finalized method... If I do that might as well go with my own temp file manager !

So long story short, can I use the Garbage Collector to clean up system resources (ie files) as well ?


Thank you all for your answers. I accepted Christoffer's as it was the simplest to implement and is what I ended up doing.

I guess being cleaned up after for so many years made me forget the basic housekeeping I was though to do the hard way in the good'ol days of C++.

like image 273
Newtopian Avatar asked Jun 29 '09 07:06

Newtopian


People also ask

Can we do garbage collection manually?

Manually starting the Garbage Collector (GC) can degrade JVM performance. See list item 4b in Interaction of the Garbage Collector with applications. The GC can honor a manual call; for example, through the System. gc() call.

How do you force an object garbage collected?

You can force object finalization to occur by calling System 's runFinalization method. System. runFinalization(); This method calls the finalize methods on all objects that are waiting to be garbage collected.

What is garbage collection explain with example?

Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.


2 Answers

Sure you can. The question is whether you really want to :)

I've actually encountered this problem in the wild; as you noticed, cleaning up temporary files with deleteOnExit() is useless when running a service rather than an application. I found that the most stable solution was to restructure the program flow such that temporary files were created on a per-task basis, and explicitly deleted when no longer needed.

If you do it any other way, i.e. if the program is unable to conclude whether a temporary file should be kept or discarded at any point during the execution, you might have a design problem. Wrapping files in some manager harness would just postpone the "real" solution ;)

like image 80
Christoffer Avatar answered Sep 18 '22 00:09

Christoffer


You might want to look into PhantomReference:

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

like image 23
Robert Munteanu Avatar answered Sep 22 '22 00:09

Robert Munteanu