Reduce the number of open files in Java code

Hi, I have a code that uses a block.

RandomAccessFile file = new RandomAccessFile("some file", "rw");
FileChannel channel = file.getChannel();

// some code
String line = "some data";
ByteBuffer buf = ByteBuffer.wrap(line.getBytes());
channel.write(buf);

channel.close();
file.close();

but the specification of the application is that I have to generate a large number of temporary files, more than 4000 on average (used for Hive attachments in a partitioned table).

The problem is that sometimes I see an exception

Failed with exception Too many open files

while the application is running.

I figured out if there is a way to tell the OS that the file is already closed and no longer in use, why

channel.close();
file.close();

does not reduce the number of open files. Is there a way to do this in Java code?

I have already increased the maximum number of open files in

#/etc/sysctl.conf:
kern.maxfiles=204800
kern.maxfilesperproc=200000
kern.ipc.somaxconn=8096

Update: I tried to fix the problem, so I broke up with the code to examine every part of it (create files, upload to the hive, delete files).

'File' 'RandomAccessFile' " ".

, :

FileOutputStream s = null;
FileChannel c = null;

try {
    s = new FileOutputStream(filePath);
    c = s.getChannel();
    // do writes
    c.write("some data"); 
    c.force(true);
    s.getFD().sync();

} catch (IOException e) {
    // handle exception
} finally {
    if (c != null)
        c.close();
    if (s != null)
        s.close();
}

( 20 5 ). . ( ) - . , , JDBC. .

+5
3

, , - , , . unix . , - 1024 JVM.

a) ulimit , JVM . (- 'ulimit -n 4000')

b) , , , "".

+4

finally {}. - , , , .

+3

? , , , . , .

0
source

All Articles