시작하기 전에 연습 1.3: 누수 후보 분석을 완료해야 합니다.
이제 2차 큐가 누수되는 큐임을 알았습니다. 소스 코드를 확인하여 문제점을 찾아보겠습니다.
2차 큐의 소스를 열고 메모리 누수를 수정하려면 다음을 수행하십시오.
public class SecondaryQueue { private Vector myQ; private int currentPos; public SecondaryQueue(){ myQ = new Vector(); currentPos=0; } public void add(Object obj){ myQ.add(obj); } public Object getNext(){ // First In First Out. // Get an object exactly once, // currentPos keeps track of last item removed. if(myQ.size()>currentPos){ currentPos++; return myQ.get(currentPos-1); } } }
public class SecondaryQueue { private Vector myQ; private int currentPos; public SecondaryQueue(){ myQ = new Vector(); currentPos=0; } public void add(Object obj){ myQ.add(obj); } public Object getNext(){ if(myQ.size()>0){ return myQ.remove(0); } return null; } }
기본 메뉴에서 파일 > 저장을 선택하여 변경 내용을 저장하십시오.
전과 같이 힙 덤프를 캡처하여 어플리케이션을 다시 프로파일링하십시오. 누수를 분석할 때 누수 후보 보기에 "누수 분석 결과, 누수 후보가 없습니다."가 표시됩니다. 2차 큐의 누수를 해결했으며 누수 후보 알고리즘이 추가 누수를 발견하지 않았습니다.
요약에 있는 내용을 검토하여 이 학습을 완료하십시오.