JVM의 힙 영역이 오염된 상태를 의미.
자바 메모리 구조
힙영역
: 자바 프로그램에서 사용되는 모든 "인스턴스 변수(객체)" 들이 저장되는 영역!
참조 블로그의 예시를 활용하여 분석 해보자.
public class Main {
public static void main(String[] args) {
List<String> strs1 = Arrays.asList("첫");
List<String> strs2 = Arrays.asList("첫") ;
doSomething(strs1, strs2) ;
}
public static void doSomething(List<String> ... strLst) {
List<Integer> intList = Arrays.asList(42);
Object[] objects = strLst;
objects[0] = intList; // 힙 오염
String s = strLst[0].get(0); // ClassCaseException
}
}
위의 코드를 보면 넘어온 List<String>
배열.
-> 해당 가변 인자를 Object[]
배열 변수로 초기화
-> 인덱스 0의 요소를 List<Integer>
타입의 변수로 초기화.
이때 힙 오염이 발생!
해당 배열에 다른 타입이 공존하기 때문에
그리고 마지막 요소 꺼낼 때 예외 발생. 보이지 않는 형 변환이 숨어 있기 때문에
String s = strLst[0].get(0);
==
String s= (Integer)strLst[0].get(0);