Pi4j genereates lots of threads
See original GitHub issueI 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)
public DeviceData readData() throws DeviceException{
try{
DeviceData data = new DeviceData();
if (serialPort == null || "".equals(serialPort))
serialPort = Serial.DEFAULT_COM_PORT;
serial = SerialFactory.createInstance();
serial.addListener(
event -> {
// print out the data received to the console
String payload;
try {
payload = event.getAsciiString();
LOGGER.debug("Arduino said :" + payload);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
);
LOGGER.info("Attempting connection to " + serialPort + " , baud rate " + baudRate);
serial.open(serialPort, baudRate);
if (serial.isOpen()){
LOGGER.info("Connected");
// send request, get data
LOGGER.debug("Probing");
serial.write('D');
Thread.sleep(3000);
LOGGER.debug("Requesting FREE MEMORY");
serial.write('F');
Thread.sleep(1500);
serial.close();
SerialFactory.shutdown();
LOGGER.info("Disconnected");
}
return data;
} catch (IOException e) {
throw new DeviceException("Error while connecting to device.", e);
} catch (Exception e) {
throw new DeviceException("Unexpected error while connecting to device.", e);
} finally {
}
}
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 ?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:17 (9 by maintainers)
Top Results From Across the Web
Creating too many threads in Java - Stack Overflow
I have a method (which is not in a threaded class), which creates a thread pool with given size (10-15 maximum) and uses...
Read more >Creating Threads and Multithreading in Java - DZone
Here's a brief introduction to Java Thread concepts that many people find tricky like multithreading and concurrency.
Read more >Introduction to Thread Pools in Java - Baeldung
The ThreadPoolExecutor is an extensible thread pool implementation with lots of parameters and hooks for fine-tuning. The main configuration ...
Read more >Java threads – may not be memory effecient?
We created a simple java program that will create 1000 threads. ... So that they will consume a lot more memory than the...
Read more >Java Thread Dump - VisualVM, jstack, kill -3, jcmd | DigitalOcean
Java Thread dump is list of all the threads active in the JVM. ... Above are four different ways to generate thread dump...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nevermind on the timing comment … I completely overlooked the sleep you have in the code.
Excellent news, thanks a lot