Copying files using Java NIO on Linux is slower

I want to copy large files from my local computer to Samba-Server (in the same GBit-LAN). When using my own File-Manager (Nemo on Ubuntu), the file is copied at a speed of ~ 45 MB / s. But when using Java, the speed is always lower than 8 MB / s.

Here is my sample code that uses the preferred Java method Files.copy:

public static void main(String[] args) throws IOException { Path src = Paths.get(args[0]); Path dst = Paths.get(args[1]); System.out.println("Copy " + src); System.out.println("To " + dst); long start = System.currentTimeMillis(); Files.copy(src, dst); long end = System.currentTimeMillis(); double took = end - start; took /= 1000; System.out.println("Speed: " + getReadableSpeed(((double) src.toFile().length()) / took, 2) + "\n"); } 

And here is the conclusion:

 Local To Local Copy /media/files/bigfile.avi To /media/files/bigfile.avi.bak Speed: 68 MB/s Local To NAS Copy /media/files/bigfile.avi To /home/biggie/.gvfs/nas/backup/bigfile.avi Speed: 7,7 MB/s 

When copying files from the local computer to the local computer, the speed is excellent. However, when the target is a NAS, speed slows dramatically (using OpenJDK 7 and Oracles JDK 7).

On Windows, I get better speed:

 Local To NAS Copy Z:\files\bigfile.avi To Y:\backup\bigfile.avi Speed: 37 MB/s 

Do you have any idea why file transfer on Linux using Java is slow? Or better: do you have a solution for this ?:-)

A workaround may be to invoke “cp” or “rsync -progress” in the Java language. But I would prefer only the Java method :-)

PS: Copying manually by reading from FileInputStream and writing to FileOutputStream is also slower (<8MB / s) .; -)

+4
source share
1 answer

Java will access the SMB resource through a regular file system mount. It is possible that Nemo accesses the SMB resource using another mechanism that is faster / better configurable.

In any case, if the file transfer speed is crucial, you are likely to get the best results with an external application. (Exactly which one will be OS specific ... and goes beyond StackOverflow.)


I briefly reviewed how Files.copy implemented. It appears that the actual copying of the files is implemented using the native code method ( sun.nio.fs.UnixFileCopy.transfer(...) ). Based on this, I do not think that the utility that copied the large file (via the usual mount of the file system) should go much faster than Files.copy(...) . He will need to access it differently in order to achieve significant acceleration.

+1
source

Source: https://habr.com/ru/post/1488526/


All Articles