Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

File.delete takes time

Thread view: 
aaronfude@gmail.com - 17 Oct 2006 03:43 GMT
In windows (not sure about unix) I find that if I iterate through files
in a dir and file.delete() each one and at the end of the loop I
immediately list the files in the directory, some of the files are
still there.

Does it take time after the delete for the files to actually go away?

Thanks!

Aaron Fude
Frank Langelage - 17 Oct 2006 07:07 GMT
> In windows (not sure about unix) I find that if I iterate through files
> in a dir and file.delete() each one and at the end of the loop I
[quoted text clipped - 6 lines]
>
> Aaron Fude

Had a similar problem some time ago.
Calling the garbage collection with System.gc() after the loop before
checking should solve this issue.
Daniel Dyer - 17 Oct 2006 20:49 GMT
>> In windows (not sure about unix) I find that if I iterate through files
>> in a dir and file.delete() each one and at the end of the loop I
[quoted text clipped - 7 lines]
> Calling the garbage collection with System.gc() after the loop before  
> checking should solve this issue.

Calling System.gc() is rarely a solution for anything.  It's not even  
guaranteed to do anything.  I'd guess that it just makes it look like  
things are working.  The real problem is likely to still be lurking  
somewhere.

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

Mark Jeffcoat - 17 Oct 2006 23:08 GMT
>>> In windows (not sure about unix) I find that if I iterate through files
>>> in a dir and file.delete() each one and at the end of the loop I
[quoted text clipped - 12 lines]
> things are working.  The real problem is likely to still be lurking
> somewhere.

I'll second that.

If System.gc() does occasionally appear to help here, it likely
means that you're leaking open file handles somewhere. It may
seem convenient to let the garbage collector do your close()ing
for you, but it'll introduce serious problems if you try to, say,
delete or rename the file that still has an open FileInputStream
on it.

The exact kind of problem varies by platform, making this one
especially fun.

Signature

Mark Jeffcoat
Austin, TX

Robert Mabee - 18 Oct 2006 01:03 GMT
>> In windows (not sure about unix) I find that if I iterate through files
>> in a dir and file.delete() each one and at the end of the loop I
>> immediately list the files in the directory, some of the files are
>> still there.
> Calling the garbage collection with System.gc() after the loop before
> checking should solve this issue.

If a Java object does something (like closing a file) when it is
finalized then that action is unpredictably delayed and can cause
surprising problems.  One I ran into was a DB application that worked
fine under light test and soon stopped under load -- it turned out
that a SQL query held onto an item of a limited pool in the DB server
until garbage collection called the finalizer.  The fix was to
explicitly close or finalize the query object before leaving the
function that used it.

Explicitly calling the garbage collector is a good way to detect this
class of problem and a bad way to fix it, both because it's expensive
and because it isn't guaranteed to do anything -- it's a "suggestion"
to the JVM.

However, I don't see a finalizer or close() in java.io.File.  It would
be a significant bug if File's implementation held a system resource
like a file handle.  Perhaps you are seeing an OS feature?
Mark Jeffcoat - 18 Oct 2006 01:30 GMT
> If a Java object does something (like closing a file) when it is
> finalized then that action is unpredictably delayed and can cause
[quoted text clipped - 13 lines]
> be a significant bug if File's implementation held a system resource
> like a file handle.  Perhaps you are seeing an OS feature?

Sorry for the confusion; I was talking about problems with
unclosed FileInputStreams (and similar classes).

One idea I stole from Bruce Eckel's _Thinking in Java_ is a
way to use the finalizer to detect these problems. You can't
reliably use it to close an open stream, or return a resource
to a pool, but you can do something like

   protected void finalize() throws Throwable {
       assert(!resource.open());
   }

in hopes of finding a place where you've broken the rules.
(I say hope because, of course, there's no guarantee that
the garbage collector will ever even run. This is a check,
not an excuse to stop paying attention.)


Signature

Mark Jeffcoat
Austin, TX

Daniel Dyer - 17 Oct 2006 20:47 GMT
> In windows (not sure about unix) I find that if I iterate through files
> in a dir and file.delete() each one and at the end of the loop I
> immediately list the files in the directory, some of the files are
> still there.
>
> Does it take time after the delete for the files to actually go away?

Did you check the return value of the delete method?  It returns a boolean  
indicating whether the delete was successful or not.

Dan.

Signature

Daniel Dyer
http://www.uncommons.org



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.