我使用Formscanner和新的本地線程其處理一些圖片給它的錯誤後:異常線程「main」 java.lang.OutOfMemoryError:無法創建
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 & D.但我沒有成功。
您正在運行32位或64位JVM嗎?什麼是操作系統使用?內存設置是什麼(Xmx,Xms,Xss ...)? –
池被設想限制運行的線程數,並且你有一個等待每個線程結束的'thread.get'。所以除非你在新線程中調用這個方法(這在stacktrace中是可見的),我不明白這會如何超載。 **但是,當然,它可能是第一個運行的FieldDetector實例,它將所有內存都提供給**。 FieldDetector實例會做什麼? – AxelH
即時通訊使用Ubuntu的64位與openjdk – Baran