简体   繁体   中英

Very slow download speed using Java in Raspberry Pi 3

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM