Pet*_*nan 5 android memory-leaks text-to-speech google-text-to-speech
由于内存不足(在程序中,而不是程序员),我一直在进行应用程序崩溃.MAT显示我的Activity的副本有时会在屏幕旋转中保留,而保持虚假副本的唯一对象是每个实例的TextToSpeech对象.我可以使用此代码段复制此行为:
public class MainActivity extends Activity {
TextToSpeech mTts;
char[] mBigChunk = new char[1000000]; // not used; just makes MainActivity instances easier to see in MAT
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onStart() {
super.onStart();
if (mTts==null) // shouldn't be necessary and doesn't make any difference
mTts = new TextToSpeech(this, null); // commenting this out fixes the leak
}
@Override
public void onStop() {
super.onStop();
if (mTts != null) {
mTts.shutdown();
mTts = null; // shouldn't be necessary and doesn't make any difference
}
}
}
Run Code Online (Sandbox Code Playgroud)
在30个方向更改后,MAT列出了net.catplace.tts_leak.MainActivity的一到八个实例,以及各种TTS对象的多个实例; 例如:
Class Name | Shallow Heap | Retained Heap | Percentage
------------------------------------------------------------------------------------------------------------------
android.speech.* | | |
android.speech.tts.TextToSpeech$Connection$1 @ 0x42de94c8 Native Stack| 24 | 2,052,664 | 11.85%
android.speech.tts.TextToSpeech$Connection$1 @ 0x431dd500 Native Stack| 24 | 2,052,664 | 11.85%
android.speech.tts.TextToSpeech$Connection$1 @ 0x435cc438 Native Stack| 24 | 552 | 0.00%
android.speech.tts.TextToSpeech$Connection @ 0x441b3698 | 32 | 528 | 0.00%
android.speech.tts.TextToSpeech @ 0x43fb3c00 | 64 | 496 | 0.00%
android.speech.tts.TextToSpeech$Connection @ 0x43fb4420 | 32 | 48 | 0.00%
android.speech.tts.TextToSpeech$Connection$1 @ 0x43fb4440 Native Stack| 24 | 24 | 0.00%
android.speech.tts.TextToSpeech$Connection$1 @ 0x441b36b8 Native Stack| 24 | 24 | 0.00%
Total: 8 entries (13,079 filtered) | | |
------------------------------------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
MAT表示TTS保留了MainActivity的伪造副本:
Class Name | Shallow Heap | Retained Heap
---------------------------------------------------------------------------------------------------------------------
| |
net.catplace.tts_leak.MainActivity @ 0x437c6068 | 200 | 2,001,352
'- mContext android.speech.tts.TextToSpeech @ 0x431de6d8 | 64 | 496
'- this$0 android.speech.tts.TextToSpeech$Connection @ 0x441b3698 | 32 | 528
'- this$1 android.speech.tts.TextToSpeech$Connection$1 @ 0x441b36b8 Native Stack| 24 | 24
---------------------------------------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
我在一系列真实设备和AVD中获得了这种行为.以上结果来自Nexus 7.
我尝试过不同的TTS引擎,使用不同的事件来创建和销毁mTts等.
我的假设是TextToSpeech并不总是使其对创建它的上下文的引用无效,从而导致泄露的上下文副本(Activity).但我是新来的; 有什么我做错了吗?
如果没有实现, android:configChanges="orientation"那么你可以重写onDestory方法:
@Override
protected void onDestroy() {
super.onDestroy();
if (ttsEngine != null) {
ttsEngine.stop();
ttsEngine.shutdown();
Log.d(TAG, "TTS destroyed");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1406 次 |
| 最近记录: |