Androidのよくあるメモリの漏洩と解決方法

1221 ワード

1.あなたのConteextに注意して引用してください.
適当なcontextを使ってみよう.
(1)Toastは多くのActivityで見られます.getAppliation Conteet()(2).serviceはactivityよりもライフサイクルが長いので、以下のコードを使います.
Intent mysService=new Intent(getApplication Conttext()、MyService.class)
どのような場合にContectsを使うべきですか?
2.serviceを閉じているか確認する
例えばGoogle位置serviceを使っていますが、google AppliClient.disconnectを呼び出し忘れてしまいました.

//Disconnect from API onDestroy()
    if (googleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, GoogleLocationService.this);
        googleApiClient.disconnect();
    }
3.imageとbitmapsの使用を確認する
スクウェア'sの第三者ライブラリPicassoを使用すると、コールしないとメモリが漏れてしまいます.この関数は私のメモリを50 Mから19 M以下に減らします.

Picasso.with(ActivityExample.this)                   //Activity context
                .load(object.getImageUrl())           
                .fit()                                //This avoided the OutOfMemoryError
                .centerCrop()                         //makes image to not stretch
                .into(imageView);

4.broadcast receivers
broadcast receiversを使って登録を解除します.
5.Observer
java.util.Observerを使えば、
deleteObserverを起動することを確認します.