OutOfMemoryError:无法使用ExecutorService创建新的本机线程

时间:2013-10-15 07:07:05

标签: java multithreading executorservice

我一夜之间启动了我的实例以了解它是如何处理事情的,当我今天早上来到时,我正面临着

Exception in thread "pool-535-thread-7" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:691)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
    at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:992)[info] application - Connecting to server A
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

我的代码的目标非常简单:每隔5分钟,我连接到远程服务器列表,发送请求(通过套接字),就是这样。

这是我的代码:

我的“cron”任务:

/** will create a new instance of ExecutorService every 5 minutes, loading all the websites in the database to check their status **/
/** Maybe that's where the problem is ? I need to empty (GC ?) this ExecutorService ? **/
Akka.system().scheduler().schedule(
    Duration.create(0, TimeUnit.MILLISECONDS), // Initial delay 0 milliseconds
    Duration.create(5, TimeUnit.MINUTES),     // Frequency 5 minutes
    new Runnable() {
        public void run() {
            // We get the list of websites to check
            Query<Website> query = Ebean.createQuery(Website.class, "WHERE disabled = false AND removed IS NULL");
            query.order("created ASC");
            List<Website> websites = query.findList(); // Can be 1, 10, 100, 1000. In my test case, I had only 9 websites.

            ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
            for (Website website : websites) {
                CheckWebsite task = new CheckWebsite(website);
                executor.execute(task);
            }

            // This will make the executor accept no new threads
            // and finish all existing threads in the queue
            executor.shutdown();
        }
    },
    Akka.system().dispatcher()
);

我的CheckWebsite类:

public class CheckWebsite implements Runnable {
    private Website website;

    public CheckWebsite(Website website) {
        this.website = website;
    }

    @Override
    public void run() {
        WebsiteLog log = website.checkState(); // This is where the request is made, I copy paste the code just after
        if (log == null) {
            Logger.error("OHOH, WebsiteLog should not be null for website.checkState() in CheckWebsite class :s");
            return;
        }

        try {
            log.save();
       catch (Exception e) {
            Logger.info ("An error occured :/");
            Logger.info(e.getMessage());
            e.printStackTrace();
        }
    }
}

checkState()中的Website.class方法:

public WebsiteLog checkState() {
    // Since I use Socket and the connection can hang indefinitely, I use an other ExecutorService in order to limit the time spent
    // The duration is defined via Connector.timeout, Which will be the next code.

    ExecutorService executor = Executors.newFixedThreadPool(1);

    Connector connector = new Connector(this);
    try {
        final long startTime = System.nanoTime();

        Future<String> future = executor.submit(connector);
        String response = future.get(Connector.timeout, TimeUnit.MILLISECONDS);

        long duration = System.nanoTime() - startTime;

        return PlatformLog.getLastOccurence(this, response, ((int) duration/ 1000000));
    }
    catch (Exception e) {
        return PlatformLog.getLastOccurence(this, null, null);
    }
}

这是Connector.class。我在这里删除了无用的部分(如Catches):

public class Connector implements Callable<String> {
    public final static int timeout = 2500; // WE use a timeout of 2.5s, which should be enough

    private Website website;

    public Connector(Website website) {
        this.website = website;
    }

    @Override
    public String call() throws Exception {
        Logger.info ("Connecting to " + website.getAddress() + ":" + website.getPort());
        Socket socket = new Socket();

        try {
            socket.connect(new InetSocketAddress(website.getIp(), website.getPort()), (timeout - 50));
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String response = input.readLine();
            socket.close();

            return response;
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        finally {
            // I take the precaution to close the socket here in order to avoid a memory leak
            // But if the previous ExecutorService force the close of this thread before
            // I can't guarantee it will be closed :/
            if (socket != null && !socket.isClosed()) {
                socket.close();
            }
        }
    }
}

我是Java多线程的新手,所以我可能犯了大错。我怀疑某些领域可能是原因,但我缺乏知识需要我寻求帮助:)

总结一下,这里是潜力领域:

  1. 每5分钟创建一个新的ExecutorService。也许我可以重用旧的?或者我是否需要在完成时关闭当前的一个(如果是,如何?)。
  2. 我创建的ExecutorService将创建ExecutorServicecheckstate()方法)
  3. 连接器类可以({1}}运行它(暴力)停止,如果它需要太长时间,导致套接字未关闭(然后内存泄漏)?
  4. 此外,正如您所看到的,线程“pool-535-thread-7”发生了异常,这意味着它很快就不会发生。

    我将last_occured支票存储在数据库中,并创建日志条目(在ExecutorService中),增量约为5小时(因此,每隔5分钟,线程在大约60次调用后崩溃)

    更新:这是重新审核的checkState方法,包含关机调用:

    WebsiteLog

1 个答案:

答案 0 :(得分:6)

我不确定这是唯一的问题,但您在ExecutorService方法中创建checkState(),但不要将其关闭。

根据Executors.newFixedThreadPool()的JavaDocs:

  

池中的线程将存在,直到明确关闭。

线程保持活动状态会导致ExecutorService not to be garbage collected(代表您调用shutdown()。因此,每次调用此线程时都会泄漏一个线程。