【问题标题】:heap size increase upto 35mb when application started应用程序启动时堆大小增加到 35mb
【发布时间】:2013-02-16 00:50:59
【问题描述】:

我的应用程序包含多达 40 个带有 5 个选项卡的活动。每个活动都有一个背景图像。当我进入一个活动时,该活动的背景图像已被加载并显示。当我离开该活动时,我正在onDestroy 方法中回收该图像。

上述过程运行良好,即加载位图和回收位图

为了加载图像,我使用以下步骤。

我将缓存目录用于图像路径,将哈希图用于以图像路径为键的位图。

  • 1) 最初加载图像时,我正在使用该图像路径检查哈希图,如果它存在,那么我只需将该位图设置为图像。

  • 2) 如果哈希图不包含位图或位图已经回收,那么我正在检查缓存的路径可用性。

  • 3) 如果缓存包含,那么我将使用这些 url 哈希码创建位图。

回收:

  • 1) 我正在使用该图像路径检查哈希图并回收位图。

我的问题是

  • 1) 当我在几秒钟内启动应用程序时,堆大小会增加到 25 到 30 mb,这会在几分钟内导致 OutOfMemeory

  • 2) 当我要进行活动时,堆大小会增加以加载图像,即使我正在维护缓存和哈希映射。

有没有什么办法可以最小化堆内存和其他存储位图的方法。

谁能帮助我如何面对这种情况

我在下面发布了一些代码。

提前致谢 文卡特。

我的代码是:

public ConcurrentHashMap<String, WeakReference<Bitmap>> cache=new ConcurrentHashMap<String, WeakReference<Bitmap>>();

public void DisplayImage(String url,Activity activity, View imageView,int width,int height)
{
    this.REQUIRED_WIDTH = width;
    this.REQUIRED_HEIGHT = height;

    if(cache.containsKey(url) && cache.get(url).get() != null && !cache.get(url).get().isRecycled())
    {
        if(imageView instanceof ImageView)
        {
            ((ImageView)imageView).setImageBitmap(cache.get(url).get());
        }
        else
            imageView.setBackgroundDrawable(new BitmapDrawable(cache.get(url).get()));

    }
    else
    {
        if(cache.containsKey(url))
        {
            cache.remove(url);
        }
        queuePhoto(url, activity, imageView);
        if(imageView instanceof ImageView)
        {
            ((ImageView)imageView).setBackgroundResource(stub_id);
        }
        else
        {
                        imageView.setBackgroundColor(Color.parseColor(AppConstants.customCollection.HomeScreen));
        }
    }    
}


public void recycleBitmaps(final String backgroundImageUrl)
{
    new Handler().postDelayed(new Runnable() 
    {
        public void run()
        {
            stopThread();
            Vector<WeakReference<Bitmap>> vecBitmapRef = new Vector<WeakReference<Bitmap>>();
            if(!cache.isEmpty())
            {
                if(backgroundImageUrl!=null && !backgroundImageUrl.equalsIgnoreCase(""))
                {
                    WeakReference<Bitmap> bmpref = (WeakReference<Bitmap>)cache.get(backgroundImageUrl);
                    if(bmpref!=null && bmpref.get()!=null && !bmpref.get().isRecycled())
                    {
                        vecBitmapRef.add(cache.remove(backgroundImageUrl));
                    }
                }
                Set<String> keys = cache.keySet();
                for(String key: keys)
                {
                    vecBitmapRef.add(cache.remove(key));
                }
                    Log.e("Bitmap size", ""+vecBitmapRef.size());
                try
                {
                    for(WeakReference<Bitmap> bitmapRef : vecBitmapRef)
                    {
                        if(bitmapRef != null && bitmapRef.get() != null)
                        {
                            if(!bitmapRef.get().isRecycled())
                            {
                                bitmapRef.get().recycle();
                                bitmapRef.enqueue();
                                bitmapRef =null;
                            }
                        }
                        Runtime.getRuntime().gc();
                    }
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                }
                vecBitmapRef.clear();
                cache.clear();
            }
        }
    },500);


}

【问题讨论】:

    标签: android out-of-memory heap-memory recycle loading-image


    【解决方案1】:

    我假设每个活动都有一个 imageView。我认为某些活动及其附加视图不会被破坏,因此 ImageViews 维护对您的位图的引用,因此 bitmapRef.get() 不会返回 null,因为 imageViews 对其底层位图有很强的引用。

    要确认这个假设,您可以尝试堆转储来查看内存的累积情况。

    Android ==> Memory Analysing ==> Eclipse memory analyzer?

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2014-03-09
      • 1970-01-01
      • 2021-02-06
      • 2016-06-18
      相关资源
      最近更新 更多