나는 내 기억 문제를 해결하기 위해 15 시간을 보냈다. 그런데 그것에 대해 몇 가지 질문을 읽었지만, 나는 정말로 좋은 대답과 해결책을 찾지 못했다.처리기 메모리 누수
MediaPlayer 시간이 처리기를 사용하여 setOnBufferingUpdateListener에서 업데이트 될 때마다 두 개의 정수를 전송합니다. 정수를 두 개의 TextViews로 설정하십시오. 그러나 매번 시간이 업데이트됩니다. 안드로이드 스튜디오의 안드로이드 모니터는 메모리가 1 메가 바이트 씩 증가하는 것을 보여줍니다.
나는 그것을 원하지 않습니다. 여기 내 수업.
핸들러 : 조각에
public class UpdateH extends android.os.Handler {
private OnUpdate onUpdate;
public UpdateH() {
}
public UpdateH(Callback callback) {
super(callback);
}
public UpdateH(Looper looper) {
super(looper);
}
public UpdateH(Looper looper, Callback callback) {
super(looper, callback);
}
@Override
public void handleMessage(Message msg) {
onUpdate.update(msg.arg1, msg.arg2);
}
public void setOnUpdate(OnUpdate onUpdate) {
this.onUpdate = onUpdate;
}
public interface OnUpdate{
void update(int cu, int t);
}
}
다음 글
@Override
public void onPause() {
super.onPause();
Player.playerProgressHandler = null;
mu = null;
}
private UpdateH mu;
@Override
public void onResume() {
super.onResume();
mu = new UpdateH(Looper.getMainLooper());
mu.setOnUpdate(new UpdateH.OnUpdate() {
@Override
public void update(int cu, int t) {
setTime(cu, t);
}
});
Player.playerProgressHandler = mu;
}
private void setTime(int current, int total){
this.current.setText(String.format("%02d:%02d:%02d",
(int) ((current/(1000 * 60 * 60)) % 24),
(int) ((current/(1000 * 60)) % 60),
(int) (current/1000) % 60));
this.total.setText(String.format("%02d:%02d:%02d",
(int) ((total/(1000 * 60 * 60)) % 24),
(int) ((total/(1000 * 60)) % 60),
(int) (total/1000) % 60));
}
는 :
if(playerProgressHandler != null && mp != null && mp.isPlaying()){
Message message = Message.obtain();
message.setTarget(playerProgressHandler);
message.arg1 = mp.getCurrentPosition();
message.arg2 = mp.getDuration();
message.sendToTarget();
}
문제는 setTime 방법에서 온다. 왜냐하면 내가 그 안에있는 코드에 주석을 달았 기 때문입니다. 기억은 시원하게 유지됩니다.
주 : 이동 중지를 당신이 스레드를 실행하는 경우, 당신은 당신들의 OnDestroy에서 그것을 취소해야하는
내가 말했듯이 문제는 스레드에 관한 것이 아닙니다. 정수는 MediaPlayer.setOnBufferingUpdateListener 콜백에 의해 전송됩니다. 그것은 TextView의 settext 메소드에 관한 것입니다. – user3502626
틀렸어. 스레드에 관한거야. 스레드는 활동이 소멸 된 후에도 계속되지만 textview를 설정하는 호출로 인해 활동에 대한 참조가 있습니다. 이로 인해 전체 활동 및 연관된 모든보기 및 변수가 유출됩니다. 스레드를 수정하면 누수가 발생합니다. –
하지만 [MediaPlayer 클래스] (https://developer.android.com/reference/android/media/MediaPlayer.html)를 사용하고 있기 때문에 스레드를 수정할 수 없습니다. 내 코드는 MediaPlayer 시간이 setOnBufferingUpdateListener에서 업데이트 될 때 호출됩니다. ** TextView를 업데이트하기위한 스레드를 만들지 않았습니다. 그 스레드는 안드로이드에서와 나는 아무것도 할 수 없어. 또한, 코드가 잘 작동합니다. 왜냐하면 활동을 닫을 때 메모리가 증가하지 않기 때문에 활동이 on_Pause ** – user3502626