最近在调试项目的时候,用LeakCanary测试内存泄漏时,在安卓4.4版本发现AudioManager引起了内存泄漏,而安卓6.0则不会,经过多方查找,确定了是VideoView导致的,原来是AudioManager可能会长时间持有Context,当使用者(这里即VideoView)请求了音频的焦点却没有及时释放,以下提供2种解决方法,原理都是一样,即让AudioManager持有ApplicationContext,而不是Activity:
方法1:在java代码中初始化VideoView
VideoView videoview = new VideoView(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViesGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
videoView.setLayoutParams(params);
(RelativeLayout)findViewById(R.id.videoContainer)).addView(videoView,0);
把VideoView写在Activity的布局文件中,初始化的时候使用Activity的Context,这里直接用代码初始化VideoView,强行传ApplicationContext,就避免了Activity被持有可能导致内存泄漏。
方法2:重写getSystemService方法
考虑到AudioManager在VideoView里面的初始化方法如下:
public VideoView(Context context,AttributeSet attrs,int defStyleAttr,int defStyleRes){
super(context,attrs,defStyleAttr,defStyleRes);
...
mAudioManager = ( AudioManager ) Context.getSystemService(Context.AUDIO_SERVICE);
...
}
它是通过调用Context的getSystemService(Context.AUDIO_SERVICE)方法初始化的,那么可在Activity中重写这个方法,使用ApplicationContext来调用它,具体如下:
@Override
protected void attachBaseContext(Context new Base){
super.attachBaseContext(new ContextWrapper(newBase){
@Override
public Object getSystemService(String name){
if(Context.AUDIO_SERVICE.equals(name)){
return getApplicationContext().getSystemService(name);
}
return super.getSystemService(name);
}
});
}
这样AudioManager将持有ApplicationContext而不是Activity。
以上方法在安卓4.4以上测试OK,官方在2015年3月修复了这个bug,(即在安卓6.0后)
主要修复了两个地方,一是在VideoView中及时释放音频焦点,二是让AudioManager持有ApplicationContext而不是持有Context。