- Applicaion을 실행 하던 중 OOM(Out Of Memory)발생.
- OOM을 발생하지 않게 하기 위해 메모리 관리와 Thread관리를 했던 방식을 정리.
- JConsole로 Application을 모니터링 하던 중 지속적으로 Live Thread가 증가.
- Live Thread를 줄이기 위하여 했던 방식
- Thread Pool 사용
public class makeThread {
private final ExecutorService executorService;
Process pr = new Process();
public makeThread(){
this.executorService = Executors.newFixedThreadPool(10);
}
public void runByThreadPool(JSONObject ruleJson){
Runnable runnable = () -> {
pr.process();
};
executorService.execute(runnable);
}
public void shutdown(){
executorService.shutdown();
try {
if(!executorService.awaitTermination(30, TimeUnit.SECONDS)){
executorService.shutdownNow();
}
}catch (InterruptedException e){
executorService.shutdownNow();
}
}
public ExecutorService getExecutorService(){
return executorService;
}
}
Thread Pool을 사용하여 10개의 Thread만 생성하여 사용 후 그 다음 Thread들을 처리.
Thread Pool의 개수는 CPU core수와 비슷하거나 조금 더 적게 하는 것이 적당.
Thread가 끝난 후 shutdown()을 통하여 종료.
이렇게 내가 생성한 Thread들을 관리.
2. elaistcSearch HighlevelClient 혹은 LowlevelClient를 한번만 생성 후 종료 시에 client.close(); 해주는 방식으로 변경.