文字转语音和内存泄漏 [英] TextToSpeech and memory leak

查看:1193
本文介绍了文字转语音和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有应用程序崩溃是由于内存不足 - 的条件(在程序中,而不是程序员)。 MAT表明,我的活动副本有时被保留在整个屏幕旋转,并保持伪造复制活着的唯一目的是每个实例的文字转语音对象。我可以用这个片段复制此行为:

I've been having app crashes due to an out-of-memory condition (in the program, not the programmer). MAT shows that copies of my Activity were sometimes being retained across screen rotations, and the only object keeping the bogus copies alive was each instance's TextToSpeech object. I can duplicate this behaviour using this snippet:

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
        }
    }
}

在30取向的变化,MAT名单之一,net.catplace.tts_leak.MainActivity的8个实例,以及各种TTS的对象也多实例之间;例如:

After 30 orientation changes, MAT lists between one and eight instances of net.catplace.tts_leak.MainActivity, and also multiple instance of various TTS objects; eg:

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)                                    |              |               |           
------------------------------------------------------------------------------------------------------------------

MAT表明MainActivity的假拷贝被保留TTS:

MAT indicates that the bogus copies of MainActivity are being retained by TTS:

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
---------------------------------------------------------------------------------------------------------------------

我得到在一系列真实设备和自动真空淀积系统的这种水煤浆。上述结果是从的Nexus 7。

I get this behavour across a range of real devices and AVDs. The above results are from a Nexus 7.

我已经尝试了不同的TTS引擎,使用不同的事件来创建和销毁MTTS等。

I've tried different TTS engines, using different events to create and destroy mTts, etc.

我的假设是,文字转语音并不总是空的参照创建它,导致在上下文(活动)的泄露拷贝的上下文。但我在这个新的;是不是我做错了吗?

My hypothesis is that TextToSpeech doesn't always null its reference to the context that created it, resulting in leaked copies of the context (Activity). But I'm new at this; is there something I'm doing wrong?

推荐答案

考虑一下文字转语音<源$ C ​​$ C一href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.1_r2/android/speech/tts/TextToSpeech.java#TextToSpeech.%3Cinit%3E%28android.content.Context,android.speech.tts.TextToSpeech.OnInitListener%29\"相对=nofollow>这里,你会发现,它实际上绑定到传递的上下文和关机方法实际上取消绑定它的服务。现在休息是未来的猜想,因为服务都有自己的生命周期,你做了一些研究牢记这一点,你实际运行一个服务,你可能会破解问题的文字转语音可能会踌躇不前context.If。

Taking a look at the source code of TextToSpeech here, you'll notice that it actually binds a service to the context passed and the shutdown method actually unbinds it. Now rest ahead is guess, since Service has its own life cycle,the TextToSpeech might be holding back the context.If you did some research keeping this in mind that you're actually running a service,you might crack the question.

现在我不也知道这是什么可能意味着,但我打开从你身边的任何新发现。
由于文字转语音是你可能想通过它的应用程序上下文服务,因为该服务将仍然运行时,活动被销毁。

Now I'm not also sure what this might imply but I am open for any new findings from your side. Given that TextToSpeech is a service you might want to pass it the application context, since the service would still be running when activity gets destroyed.

也为进一步阅读 _ __ _ __ _ __ _ 的____

Also for further reading ______________

这篇关于文字转语音和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆