小编Pet*_*nan的帖子

TextToSpeech和内存泄漏

由于内存不足(在程序中,而不是程序员),我一直在进行应用程序崩溃.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; …
Run Code Online (Sandbox Code Playgroud)

android memory-leaks text-to-speech google-text-to-speech

5
推荐指数
1
解决办法
1406
查看次数