Android RelativeLayout.setBackground()与Drawable一起导致内存泄漏

Android RelativeLayout.setBackground()与Drawable一起导致内存泄漏,android,memory-leaks,android-asynctask,android-relativelayout,android-drawable,Android,Memory Leaks,Android Asynctask,Android Relativelayout,Android Drawable,我有一个内存泄漏的问题。我正在从http(800KB)下载一个图像,并使用AsyncTask将其存储在一个Drawable中。下载图像后,内存似乎正常。但是,将Drawable设置为RelativeLayout的背景图像后,内存大小将增加约8-10 MB。我正在使用getUsedMem()跟踪内存使用情况。如果我使用其中几个,Android应用程序会崩溃,并出现OutOfMemoryException。有人知道这个问题吗?如何最大限度地减少内存使用 public class Example ex

我有一个内存泄漏的问题。我正在从http(800KB)下载一个图像,并使用AsyncTask将其存储在一个Drawable中。下载图像后,内存似乎正常。但是,将Drawable设置为RelativeLayout的背景图像后,内存大小将增加约8-10 MB。我正在使用getUsedMem()跟踪内存使用情况。如果我使用其中几个,Android应用程序会崩溃,并出现OutOfMemoryException。有人知道这个问题吗?如何最大限度地减少内存使用

public class Example extends Activity {
    private ArrayList<Drawable> drawables;
    private TextView tv;
    private RelativeLayout rl1;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        drawables = new ArrayList<Drawable>();
        tv = (TextView) findViewById(R.id.textView1);
        rl1 = new RelativeLayout(this);

        // Check Memory Before Everything
        printMemory(0);

        // Image url
        String image_url1 = "https://s3-us-west-1.amazonaws.com/example/image.jpg";
        new DownloadFilesTask().execute(image_url1);
    }

    public Drawable drawableFromUrl(String url, String srcName) throws java.net.MalformedURLException, java.io.IOException  {
        InputStream is = (InputStream) new java.net.URL(url).getContent();
        Drawable drawable = Drawable.createFromStream(is, srcName);
        is.close();

        return drawable;
    }

    @SuppressLint("NewApi")
    public void addDrawable(Drawable d) {
        drawables.add(d);
        rl1.setBackground(d);
        printMemory();
    }

    private class DownloadFilesTask extends AsyncTask<String, Drawable, Drawable>   {           
        protected Drawable doInBackground(String... s)       
        {
                    Drawable bgImage = Example.this.drawableFromUrl(s[0], "src name");
                    return bgImage;
         }

        protected void onPostExecute(Drawable drawable)          {
             addDrawable(drawable);
         }
     }

    public long getUsedMem()    {
        long freeSize = 0L;
        long totalSize = 0L;
        long usedSize = -1L;
        try {
            Runtime info = Runtime.getRuntime();
            freeSize = info.freeMemory();
            totalSize = info.totalMemory();
            usedSize = totalSize - freeSize;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return usedSize;
    }

    public long getTotalMem()   {
        long totalSize = 0L;
        try {
            Runtime info = Runtime.getRuntime();
            totalSize = info.totalMemory();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return totalSize;
    }

    public void printMemory()   {
        CharSequence cs = tv.getText();
        tv.setText(cs + "\n" + 
                   "Used Mem: " + getUsedMem() + "\n" +
                   "Total Mem: " + getTotalMem() + "\n" +
                   "Drawables: " + drawables.size());
    }
}
公共类示例扩展活动{
私人ArrayList抽屉;
私家图文电视;
私人亲戚关系rl1;
@SuppressLint(“新API”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawables=新的ArrayList();
tv=(TextView)findViewById(R.id.textView1);
rl1=新的相对值(本);
//在做任何事情之前检查内存
打印存储器(0);
//图像url
字符串图像_url1=”https://s3-us-west-1.amazonaws.com/example/image.jpg";
新的下载文件任务().execute(image_url1);
}
public Drawable drawableFromUrl(String url,String srcName)抛出java.net.MalformedURLException、java.io.IOException{
InputStream是=(InputStream)新的java.net.URL(URL.getContent();
Drawable Drawable=Drawable.createFromStream(is,srcName);
is.close();
回拉;
}
@SuppressLint(“新API”)
可提取的公共无效添加(可提取d){
增加(d);
rl1.退根地(d);
printMemory();
}
私有类DownloadFilesTask扩展异步任务{
受保护的可抽出式doInBackground(字符串…s)
{
Drawable bgImage=Example.this.drawableFromUrl(s[0],“src name”);
返回图像;
}
后期执行时受保护的void(可提取){
可拉伸(可拉伸);
}
}
公共长getUsedMem(){
长自由尺寸=0升;
长总尺寸=0升;
长期使用的尺寸=-1L;
试一试{
运行时信息=Runtime.getRuntime();
freeSize=info.freemory();
totalSize=info.totalMemory();
usedSize=总尺寸-自由尺寸;
}捕获(例外e){
e、 printStackTrace();
}
返回usedSize;
}
公共长gettotalem(){
长总尺寸=0升;
试一试{
运行时信息=Runtime.getRuntime();
totalSize=info.totalMemory();
}捕获(例外e){
e、 printStackTrace();
}
返回总大小;
}
public-void-printMemory(){
CharSequence cs=tv.getText();
tv.setText(cs+“\n”+
已用内存:“+getUsedMem()+”\n+
“总内存:”+GetTotalem()+“\n”+
“Drawables:“+Drawables.size());
}
}