Re: Runtime.getRuntime().exec() very slow in Java program.
au.da...@gmail.com wrote:
I am trying to run the Runtime.getRuntime().exec(shellCMD) to copy
files on a linux system.
but the getRuntime.exec() is very slow, it can only copy around 2-10
documents/second to my target directory when I have 1000 files.
How fast should it be?
Why do you think it should be that fast instead?
Can anyone give some suggestions about my code below? thanks.
for (doc ahit : docList) {
try{
shellCMD="cp "+ srcDir + "/"+ ahit.doc_=
id + " " +
tarDumpDir; //copy xml to tmp folder
Process process = Runtime.getRuntime().=
exec
(shellCMD);
//process.waitFor();
process.getInputStream().close();
process.getOutputStream().close();
process.getErrorStream().close();
}
catch (Exception e){
logError("Copy XML fail: " + e);
}
}
I am only speculating, but several things occur to me.
How fast would a shell script run:
#!/bin/bash
for fl in $*
do
bash cp ${srcDir}/${fl} ${tarDumpDir}/
done
?
Your program has to start a shell for each file copied. Given that
you show us the "cp" command, presumably that shell has to process /
etc/profile, ~/.profile and ~/.bashrc (or equivalent) each time, not
to mention the scripts in /etc/profile.d/. Add to that the overhead
of 'Runtime#exec()'.
It would likely run faster if you either ran a single shell command to
copy all the files, or used pure Java to do the copy without using
'Runtime' at all.
With a pure Java approach, you can put each copy in its own thread to
achieve a measure of parallelism. Plus it would be portable.
--
Lew