2012-03-21 2 views
0

내 라이브러리 코드는 바이트 배열을 UI에 대기열에 알립니다. 다른 스레드는 바이트 배열을 대기열에서 제외하고 핸들러 인스턴스를 사용하여 바이트 배열을 전달하고 메시지를 보내 UI를 업데이트합니다. 핸들러를 사용핸들러에서 메모리 누수를 피하는 방법은 무엇입니까?

코드는 UI

public void run(){ 

     while(running){ 
      try { 
       byte[] msg=(byte[]) queue.getMsg(); 
       Message message=new Message(); 
       Bundle bundle=new Bundle(); 
       bundle.putByteArray("img",msg); 
       message.obj=bundle; 
       handler.sendMessage(message); 
       message=null; 


      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

를 업데이트하지만 것은 내가 5 ~ 10 분에서 OutOfMemory 예외를 얻고있다. Eclipse MAT 힙 덤프를 사용하면 힙의 90 %가 android.os.Message의 인스턴스가 더 많이 차지하고 있음을 알 수 있습니다.

답변

5

매번 새 Message가 생성됩니다. 이것은 아마 귀하의 문제로 이어질 것입니다. 당신이 일을해야하는 것은 사용 HandlerMessage 풀에서 Message를 받고 있습니다 :

Message message = handler.obtainMessage(); 

이 모든 새로운 메모리를 할당에서 당신을 유지합니다.

관련 문제