[英]background image memory leak
我發現很少有主題描述類似的問題,但找不到非常簡單的Android應用程序創建的內存泄漏解決方案:
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.reloecc.testBackground"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10"/>
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<activity android:name="TestBackgroundActivity"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ui">
</LinearLayout>
TestBackgroundActivity.java :
public class TestBackgroundActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
在更改設備的方向(NVIDIA Tegra Note 7)時,logcat注意:
cz.reloecc.testBackground I/dalvikvm-heap﹕ Grow heap (frag case) to 35.625MB for 12904976-byte allocation
每個周轉大約增加了13MB(用於最大版本的圖片)
最多達到我的堆最大容量(64MB):
cz.reloecc.testBackground E/dalvikvm-heap﹕ Out of memory on a 12904976-byte allocation.
cz.reloecc.testBackground I/dalvikvm﹕ at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
..
cz.reloecc.testBackground I/dalvikvm﹕ at cz.reloecc.testBackground.TestBackgroundActivity.onCreate(TestBackgroundActivity.java:13)
但! dpi或drawable- [X | M | L]當我從抽拉-陸地[| | M L X]刪除ui.png(其被設置為背景)的問題不會保留dpi的文件夾中的res文件夾..所以,如果我背景圖片只有一個版本,我可以將設備打開一個星期。
這是我的問題:如何處理可繪制的多個版本(設置為背景)以避免內存泄漏?
//編輯:我設法進行了幾次處置,回收,銷毀,清空資源或其持有人的嘗試,最后一次是基於Aeshang的建議:
=== 2.0版 ===
Resources.java :
public class Resources {
public Resources(Context context){
this.context = context;
}
public Drawable getImage(int id){
if(images.indexOfKey(id) < 0){
Drawable drawable = context.getResources().getDrawable(id);
images.put(id, drawable);
}
return images.get(id);
}
public void disposeImages(){
int key;
for(int i = 0; i < images.size(); i++) {
key = images.keyAt(i);
Drawable drawable = images.get(key);
if(drawable instanceof BitmapDrawable){
if(drawable instanceof BitmapDrawable){
Log.i(TestBackgroundActivity.LOG_TAG, "Recycling image " + key);
((BitmapDrawable)drawable).getBitmap().recycle();
}
}
}
}
public void disposeAll(){
disposeImages();
images.clear();
}
private SparseArray<Drawable> images = new SparseArray<Drawable>();
private Context context;
}
TestBackgroundActivity.java :
public class TestBackgroundActivity extends Activity {
public static String LOG_TAG = "[TestBG]";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resources = new Resources(getApplicationContext());
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
mainLayout.setBackgroundDrawable(resources.getImage(R.drawable.ui));
}
@Override
protected void onDestroy(){
resources.disposeAll();
super.onDestroy();
}
private Resources resources;
}
使用第三方庫加載可繪制對象
如Picasso或Universal Image Loader
他們將自動處理大位圖
來到繪畫
用戶使用1920X1080分辨率的圖像並將其放置在drawable-xxhdpi文件夾中,一切正常
好,
這絕對應該工作。 在另外兩個設備上進行了測試,而altrough應用程序則抱怨內存堆增長過快。 之后就來了。
我的整體筆記7是痛苦。 我將不得不找出這個地獄的正確來源。
// edit:很好,調用
System.gc();
在MainActivity的onDestroy()中
和應用程序永遠存在..誰知道為什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.