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) .; -)
source share