Spring Boot 기반 REST API가 메모리를 점유하여 성능 저하, 부하 증가 등의 이슈가 있을 때 참고할 만한 사항들을 정리한 글이다.
1. DB 연결을 위해 java.sql.Connection을 사용하고 close() 하지 않은 경우
java.sql.Connection conn = null;
PreparedStatement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, id, pwd);
st = conn.prepareStatement(sql);
st.setQueryTimeout(3);
/** sql 작업 */
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}finally {
try {
conn.close();// connection close
st.close();// statement close
} catch (SQLException e) {}
}
finally block에서 close를 해주어 close()가 안전하게 수행될 수 있도록 하였다.
2. BufferedReader, BufferedWriter 등의 stream을 생성하는 함수를 사용하고 close() 하지 않은 경우
BufferedReader br = null;
try {
/** 작업 수행 */
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("exception 메시지", e);
}finally {
if (br != null) {
try {
br.close();// reader close
} catch (IOException e) {
e.printStackTrace();
}
}
}
finally block에서 close를 해주어 close()가 안전하게 수행될 수 있도록 하였다.
이외에도 프로세스 내에 과도한 반복문이 있거나 같은 분석을 여러 번 반복하는 경우 request 당 분석 기간이 길어져 부하가 생길 수 있다.
또한, 특정 request(길이가 매우 길거나 예상과 다르게 인코딩된 값)에 대하여 분석에 긴 시간이 소요되는 경우는 없는지 검토해야 한다. (필요에 따라 전처리를 추가하거나 최대 분석 시간을 설정할 수 있음)
'Spring, Spring boot' 카테고리의 다른 글
[Spring] RequestBody로 요청 받기 (0) | 2023.06.02 |
---|---|
[Spring] JdbcTemplate를 사용하여 데이터베이스 연결하기 (0) | 2022.07.03 |