简体   繁体   中英

Thread performance issues for Java on Raspberry Pi

The goal of this application is to handle 800 simultaneous clients over TCP, each of them sends a 3.5kb xml every second. Each of these requests needs to be parsed (see code snipped). This happens on different threads.

The limitation of this project is that it has to run on a small Raspberry Pi3 (1.2 ghz quad core, 1gb ram). And i run into utilization issues when i increase the load above 150 simultaneous clients (80% cpu usage).

When i run this program my development machine it seems to run very well. (0-1% usage, under 150). I understand that my development machine is more powerful than the RPI, and therefore runs better. But the difference seems to be too big.

In my current setup i use Java nio to handle/read all the incoming connections. Then i use multiple threads to read the data.

当前设置

This is the simple code that currently runs on the processing thread. Also tried reading a simple byte[] array 1 byte at a time. And even reading a StaX stream. Every variation of reading that i tried, the 'read type' of operation gives gives the worst performance.

BufferedInputStream input = new BufferedInputStream(new ByteArrayInputStream(buffer.array(), 0, bytecount));
int current;
/* In this snippet input.read() is the cause of performance issues 
Reading directly from byte[] gives similar poor performance.
*/
while ((current = input.read()) != -1) {
    continue;
}

According to my profiler the Input.read() call uses a huge amount of processing power on the Pi and accounts for 97% of the total cpu time. the other 3% is the Main thread that handles the connections.

On my development machine this is almost flipped and the Main thread accounts for most of the cpu usage, 93%. And 7% goes to the processing threads.

What could cause this big difference? why is this read() call on the pi so expensive compared to my other machine, could it have something to do with memory?

Notes:

  • Pi runs raspbian linux - openjdk 1.8.0_40-internal
  • Dev machine runs win 10 - Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
  • Tried running with -Xms -Xmx flag on both machines, same result.

Turns out that the problem was a combination of both JVM and 32bits OS on the Raspberry Pi 3. When running 32bit raspbian with OpenJDK my application had very poor performance (Especially on the 'read' calls). Switching to the Oracle JVM gave me the 'better' expected performance.

However when switching to a 64bit OS (OpensSuse in my case) the performance was good with both the OpenJDK and Oracle JVM.

(Credits to @jww in the comments, for suggesting to switch to a 64 bit OS)

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