4

I wrote the following code in Java for downloading a file in a Raspberry Pi 3:

String fileUrl = "...";
URL urlObj = new URL(fileUrl);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
InputStream in = con.getInputStream();

byte[] buffer = new byte[8*1024];
long t = System.nanoTime();
int read;

while ((read = in.read(buffer)) != -1) {
    System.out.println("Read " + read + "B in " + (System.nanoTime() - t)/1000000.0 + " ms");
    t = System.nanoTime();
}

Even though I am using an 8 KB buffer, the average download speed is 1389 B in around 205 ms, which translates to 6.78 KB/s:

Download speed measurements

I also noticed that the CPU usage while executing this code is always 25%. Since the RPi's CPU has 4 cores, I asume it is using 100% of a single core. I know this is a weak processor, but downloading a file isn't a demanding task, so this odd behaviour puzzles me.

agc1993
  • 91
  • 8
  • 1
    try to increase your buffer size from 8k to 256k – Arvind Jun 25 '17 at 23:37
  • I tried that. Even with a buffer size of 1MB it didn't make a difference. – agc1993 Jun 25 '17 at 23:40
  • You can try profiling it, another quick check is to take out the timing and the printing just in case something pathological is going on there. Oh and of course, try the same download with something like curl as a control. – pvg Jun 26 '17 at 01:56
  • @pvg I tried profiling it, and the problem is in the read() method. I also tried this code in another computer with Windows and it worked fine, so there must be something in the RPi which is slowing it down... Just don't know what it is... – agc1993 Jun 26 '17 at 03:49
  • 1
    How fast is curl from the rpi? – pvg Jun 26 '17 at 03:52
  • @pvg Download speed with curl is very fast, as it should be. The problem appears only when I try to download something with Java. – agc1993 Jun 26 '17 at 15:01
  • You could try a couple of implementations that are not based on `HttpUrlConnection` - the google http client and apache http clients come to mind. – pvg Jun 26 '17 at 16:06
  • I used both libraries. Download speed is still slow and CPU usage is still 25% while downloading. – agc1993 Jun 26 '17 at 18:09
  • 1
    You could try Oracle's Java instead of OpenJava, both are pre-installed in Raspbian. – Erich Kitzmueller Jun 26 '17 at 18:31
  • 1
    Yeah that sounds like something fundamentally hosed with the jdk you're using. – pvg Jun 26 '17 at 18:35

1 Answers1

4

I finally solved the problem!

I removed OpenJDK and installed Oracle JDK:

sudo apt-get purge openjdk-8-jdk
sudo apt-get purge openjdk-8-jre
sudo apt-get autoremove
sudo apt-get install oracle-java8-jdk

If Oracle's JDK was correctly installed, running java -version should read something like this:

java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)

With this, download speeds went from around 5 KB/s to around 450 KB/s, which I expected from my 4 Mbps connection.

I reinstalled OpenJDK out of curiosity and the download speed was slow again, so this jdk was the problem, as Erich Kitzmueller and pvg suggested.

Thanks to everyone for your suggestions!

agc1993
  • 91
  • 8