线程" main"中的例外情况java.lang.OutOfMemoryError:无法创建新的本机线程

时间:2017-06-22 07:51:57

标签: java multithreading pid

我使用Formscanner并在处理了一些图片之后给出错误:

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:717)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1357)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at com.albertoborsetta.formscanner.api.FormTemplate.findPoints(FormTemplate.java:852)
at com.albertoborsetta.formscanner.model.FormScannerModel.analyzeFiles(FormScannerModel.java:562)
at com.albertoborsetta.formscanner.main.FormScanner.main(FormScanner.java:145)

搜索点方法如下:

public void findPoints(BufferedImage image, int threshold, int density,
        int size) throws FormScannerException {
    height = image.getHeight();
    width = image.getWidth();
    int cores = Runtime.getRuntime().availableProcessors();

    ExecutorService threadPool = Executors.newFixedThreadPool(cores - 1);
    HashSet<Future<HashMap<String, FormQuestion>>> fieldDetectorThreads = new HashSet<>();

    HashMap<String, FormQuestion> templateFields = template.getFields();
    ArrayList<String> fieldNames = new ArrayList<>(templateFields.keySet());
    Collections.sort(fieldNames);

    for (String fieldName : fieldNames) {
        Future<HashMap<String, FormQuestion>> future = threadPool.submit(new FieldDetector(threshold, density, size, this, templateFields.get(fieldName), image));
        fieldDetectorThreads.add(future);
    }

    for (Future<HashMap<String, FormQuestion>> thread : fieldDetectorThreads) {
        try {
            HashMap<String, FormQuestion> threadFields = thread.get();
            for (String fieldName : threadFields.keySet()) {
                FormQuestion field = threadFields.get(fieldName);
                fields.put(fieldName, field);
                for (Entry<String, FormPoint> point : field.getPoints().entrySet()) {
                    if (point.getValue() != null) {
                        pointList.add(point.getValue());
                    }
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            throw new FormScannerException(e.getCause());
        }
    }

    threadPool.shutdown();

}

在循环中调用上面的函数,并且java进程的数量增加,并且在某一点上它引发了上述异常。

调用shutdown方法后,有没有办法杀死这些线程。我不是一个java开发人员。我做了一些R&amp; D.但我没有成功。

1 个答案:

答案 0 :(得分:2)

问题来自Set<Future>用于保存每个实例以便稍后检查它们。

在聊天中,你告诉我你正在检查120.000文件。这意味着创建了许多Future,当池找到一个插槽时,它将创建一个Thread来执行Callable

由于Set持有每个实例,Thread不会被垃圾收集,这会让你泄漏。您需要删除所有已使用的Future,以便GC清除下一个Thread的内存。

使用迭代器而不是循环本身很简单,让您在使用前删除当前实例

Iterator<Future<HashMap<String, FormQuestion>>> iterator = fieldDetectorThreads.iterator();
while (iterator.hasNext()) {
    //get the next instance
    Future<HashMap<String, FormQuestion>> thread = iterator.next();
    //Remove it from the set
    iterator.remove();

    //then work on that instance just like before
    try {
        HashMap<String, FormQuestion> threadFields = thread.get();
        for (String fieldName : threadFields.keySet()) {
            FormQuestion field = threadFields.get(fieldName);
            fields.put(fieldName, field);
            for (Entry<String, FormPoint> point : field.getPoints().entrySet()) {
                if (point.getValue() != null) {
                    pointList.add(point.getValue());
                }
            }
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new FormScannerException(e.getCause());
    }
}

此解决方案未经过测试,但应该能够足够快地释放内存。

但是如果提交请求的循环需要很长时间才能结束(在检查第一个请求之前将生成120k未来),这将在每个请求被发送之前中断。

在这种情况下,可能需要将该逻辑拆分为两个线程,一个用于发送请求,一个用于检查结果,直到第一个线程结束且该组为空。

以防万一,我会在循环后添加一个关闭请求

threadPool.shutdown();

它应该没有必要,但奇怪的是我的测试程序不会没有它...即使每个线程都已经处理过,它们似乎仍然存在并阻塞主线程。