Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutorService java possible thread leak

I'm trying to run this simple class and for each cicle, I'm counting the number of java process threads.

ps huH p pid | wc -l.

I'm testion on centOS 6.5 with oracle java version 1.8.0_20.

For each cicle the number of threads is increasing of the number of availableProcessors().

It seems that garbace collector doesn't deallocate zombie threads when ExecutorService is running inside an other thread.

If I create a static ExecutorService it does not happend

private static final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());).

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class ExecutorTest implements Runnable{

private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
private static final int CICLE_NUMBERS=100;
private static final int WAIT_SECONDS=20;

@Override
public void run() {
    try{
    ArrayList<Future<DummyThread>> responses = new ArrayList<>();
    for(int i=0;i<CICLE_NUMBERS;i++)
        responses.add((Future<DummyThread>) executor.submit(new DummyThread()));
    for (Future<DummyThread> future : responses)
        future.get(WAIT_SECONDS, TimeUnit.SECONDS);
    }
    catch(Exception e){
        e.printStackTrace();
    }
    System.gc();
}

public static void main(String[] args) throws InterruptedException {
    for(;;){
        new Thread(new ExecutorTest()).start();
        Thread.sleep(2000);
    }
}
}

class DummyThread implements Runnable{

@Override
public void run() {
    System.out.println("Hello World!");
}

}
like image 544
user1535271 Avatar asked Aug 13 '26 11:08

user1535271


1 Answers

add a try/finally block inside your run() method and call shutdownNow() on the executor in the finally block.

like image 177
jtahlborn Avatar answered Aug 15 '26 00:08

jtahlborn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!