This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

System.gc() to close file descriptors (was RE: status of gcj's boehm collector?)


Boehm, Hans writes:
 > > From: Andrew Haley [mailto:aph@cambridge.redhat.com]
 > > Boehm, Hans writes:
 > >  > Interesting.  Unfortunately, I think the standard Java 
 > > interface isn't quite
 > >  > sufficient to do that right.  I think the logic to do this 
 > > properly is:
 > >  > 
 > >  > if (I'm out of file descriptors and need one, but not before) {
 > >  >   for (;;) {
 > >  > 	System.gc();
 > >  > 	System.runFinalization();
 > >  > 	if (the preceding call ran no finalizers || file descriptor is
 > >  > available) break;
 > >  >   }
 > >  > } 
 > >  > 
 > >  > The loop is needed because running finalizers may result in
 > >  > dropping more objects.  If finalization dependencies are correctly
 > >  > enforced (in Java the client code needs to do that), that's even
 > >  > reasonably likely.  I think this mean you really need
 > >  > runFinalization to return an indication of whether or not it did
 > >  > anything so that you can avoid a potentially infinite loop here.
 > > 
 > > What I'm wondering is whether this is a real or a theoretical
 > > requirement for gcj.  So, for our current implementation, is there any
 > > chance that calling System.gc() might not call finalizers for files
 > > that are garmbge before returing? 

I can confirm that this really is broken in the current libgcj.

Here's a test case that I'll commit.  I'm working on a fix.

Andrew.

import java.io.*;
public class FileHandleGcTest
{
  static void kill () throws FileNotFoundException
  {
    for (int i = 0; i < 65536; i++)
      {
	FileInputStream f = new FileInputStream ("/dev/null");
      }
  }

  public static void
  main (String argv [])
  {
    try
      {
	kill ();
      }
    catch (FileNotFoundException _)
      {
	System.err.println (_);
      }
  }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]