- obsolete reference(쓸모 없어진 참조), unintentional object retention(의도하지 않은 객체 보유)
- 더 이상 쓰일 일 없는 객체를 garbage collection 하지 못하는 오류
- 코드 예제
class MyStack<E> {
private static final int MAX_SIZE = 16;
private final E[] elements;
private int position = 0;
public MyStack() {
elements = (E[]) new Object[MAX_SIZE];
}
public void push(E e) {
if(position == MAX_SIZE) throw new IllegalStateException("Full Stack");
elements[position++] = e;
}
public E pop() {
if(position == 0) throw new IllegalStateException("Empty Stack");
return elements[--position];
// 반환 객체의 reference가 elements 배열에 여전히 유지
// 스택에 다른 객체가 쌓이기 전까지 이전 객체 Garbage collection 안됨}
}
- 해결안: 더 이상 유지할 필요 없는 reference를 폐기함 (null값 설정)
public E pop() {
...
E e = elements[--position];
elements[position] = null; // obsolete reference 제거
return e;
}
- 출처: Effective Java 2판, 규칙7. 유효하지 않은 객체 폐기
'아는 만큼 보인다 > Tip' 카테고리의 다른 글
enum 자료형 흉내내기 (0) | 2015.01.15 |
---|---|
Java HotSpot VM Options 몇 가지 (0) | 2014.12.18 |
Java Memory Model (0) | 2014.11.20 |
문자열 속 일부 유니코드 치환 (0) | 2014.10.07 |
eclipse에서 java compiler 변경시.. 에러 (0) | 2013.01.08 |