question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pi4j genereates lots of threads

See original GitHub issue

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)

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.

pi4j

Is this a memory leak ? Should I reuse SerialFactory object ? Should I also reuse Serial object ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
savageautomatecommented, Sep 5, 2017

Nevermind on the timing comment … I completely overlooked the sleep you have in the code.

0reactions
menazordcommented, Sep 5, 2017

Excellent news, thanks a lot

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found