-
Notifications
You must be signed in to change notification settings - Fork 446
Pi4j genereates lots of threads #361
Comments
Yes this does look like a bug. It looks like there is an executor service that gets created each time you get a serial instance from the factory and it is not getting shutdown when you make your call to While this is a bug we need to look into, I also believe your program should only create one Serial instance and simply re-use it as needed. Calling |
Thank you for your input! Let me try to put those optimizations in place and I'll post back the results I get. |
I also noticed that you are writing some serial data and then immediately closing the connection. This may not be enough time to read the response back from the connected Arduino Uno.
Also, you mentioned USB and you also mentioned Else, if you are trying to use the USB on the RPi to communicate via serial, then |
Nevermind on the timing comment ... I completely overlooked the sleep you have in the code. |
Turns out I am able to get response from arduino every time using this code Connection between Pi and Arduino is made using USB A/B cable from Raspberry Pi USB port to Arduino Uno USB port. This USB device shows up as /dev/ttyACM0 on Raspbian
This is the output
Note that thread "pi4j-single-executor-0" also seeems to be related with the SerialDataEventListener output (4th line) |
Interesting, I thought that |
Nevermind, internal UART is |
There is a single thread executor service created for the serial instance and his job to is dispatch serial data events out to serial data listeners on this dedicated (event) thread. The intent here is to prevent blocking on the main thread that is reading the serial data from the native C implementation. I'm not sure why the shutdown call was not being honored properly. In looking at the code, I really don't like some of the way its implemented, so I may refactor it a bit. Just to make sure .. are you running version 1.2-SNAPSHOT? Thanks, Robert |
Hi Robert True, one letter, different worlds :) I was also checking the code last night to understand how the executor service works but did not find a reason why I'm currently using version 1.1 , as this is the latest available on Maven (I use maven repositories for my Gradle builds) OH, one thing: If |
1.2 snapshot has a lot of fixes and I'm working on getting it released soon. Any changes/fixes I make will be in these builds. You can easily switch to include the snapshot version in your Maven build. See the snapshot builds section here for the additional repo you would need to include:
|
Great! Let me try with the SNAPSHOT and I'll share my results Thank you! |
I think I have a clue
This boolean controls shutting down executors, so when you create a new Serial using This is why I think adding Hope this helps, I'm using 1.2.SNAPSHOT now |
Also reusing |
OK... I was able to sort this out, performing several tweaks to code
Results are really good in terms of less JVM memory. Also only 1 thread is running after 50 iterations Thanks a lot for your suggestions Robert, and congratulations for your work on this project |
Good catch on the |
A new snapshot build will include this fix in about an hour. |
Excellent news, thanks a lot |
I have some doubts using Pi4J.
I'm currently using my Raspberry Pi (Model B) to communicate via USB to my Arduino Uno board. For this purpose I'm using Pi4j, thorugh serial port /dev/ttyACM0
My serial communication code looks like this (this function is executed every 5 minutes)
Even when using both serial.close() and SerialFactory.shutdown(), I can see on Java Mission Control that there are a lot of waiting threads with the same name (pi4j-single-executor-0) and increasing over time.
Is this a memory leak ?
Should I reuse SerialFactory object ?
Should I also reuse Serial object ?
The text was updated successfully, but these errors were encountered: