我正在开发一个音乐应用程序,其中使用了Seekbar。我有一种方法可以处理搜索栏性能,并且效果很好。但是如果搜索栏正在运行,则存在内存泄漏,并且日志猫显示如下
GC_CONCURRENT freed 1692K, 34% free 10759K, paused 3ms+8ms
当搜索栏进行时,这会不断出现
我发现问题是由于TextView的动态更新,这是为了显示歌曲的当前持续时间。
我该如何解决这个问题。 请帮助我解决这个问题
我的函数是
public void seekbarProgress(){
handler.postDelayed(new Runnable() {
@Override
public void run() {
//current position of the play back
currPos = harmonyService.player.getCurrentPosition();
if(isPaused){
//if paused then stay in the current position
seekBar.setProgress(currPos);
//exiting from method... player paused, no need to update the seekbar
return;
}
//checking if player is in playing mode
if(harmonyService.player.isPlaying()){
//settting the seekbar to current positin
seekBar.setProgress(currPos);
//updating the cuurent playback time
currTime.setText(getActualDuration(""+currPos));
handler.removeCallbacks(this);
//callback the method again, wich will execute aftr 500mS
handler.postDelayed(this, 500);
}
//if not playing...
else{
//reset the seekbar
seekBar.setProgress(0);
//reset current position value
currPos = 0;
//setting the current time as 0
currTime.setText(getActualDuration(""+currPos));
Log.e("seekbarProgress()", "EXITING");
//exiting from the method
return;
}
}
}, 500);
}
GC_CONCURRENT
行并不意味着您有内存泄漏。这只是垃圾回收器清理未使用的内存。
编辑这一行:
currTime.setText(getActualDuration(""+currPos));
不会产生内存泄漏(除非getActualDuration()
做了一些有趣的事情)。它只是每 500 毫秒创建一个新String
,但它不是内存泄漏。旧的将被垃圾收集,就像您的情况一样。