Note that there are some explanatory texts on larger screens.

plurals
  1. POheap size increase upto 35mb when application started
    text
    copied!<p>My application contains so many activities up to 40 with 5tabs. and each activity has one background image. when i entered into one activity, background image for that activity has been loaded and displayed.when i away from that activity i am recycling that image in <code>onDestroy</code> method.</p> <p>the above process is working perfectly i.e <em>loading of bitmaps and recycle of bitmaps</em> .</p> <p>for loading images i am using following steps.</p> <p>I am using cache directory for image path and hash map for bitmaps with image path as a key.</p> <ul> <li><p>1) while loading image initially i am checking hash map with that image path if it is there then i am simply setting that bitmap to image.</p></li> <li><p>2) if hash map doesn't contains bitmap or bitmap has already recycled then i am checking cache for its path availability.</p></li> <li><p>3) if cache contains then i am creating bitmap with those url hash code.</p></li> </ul> <p>For recycling:</p> <ul> <li>1) i am checking in hash map with that image path and recycling the bitmap.</li> </ul> <p>My problems are</p> <ul> <li><p>1) when ever i am launching application with in few seconds the heap size is increasing to 25 to 30 mb which causes <code>OutOfMemeory</code> in few minutes.</p></li> <li><p>2) when ever i am going to activity heap size is increasing to load the image even though i am maintaining cache and hash map.</p></li> </ul> <p>Is there any way to minimize the heap memory and any other methods to store bitmaps.</p> <p>can any one please help me how to face this situation </p> <p>I am posting some code below.</p> <p>Thanks in advance Venkat.</p> <p>My Code Is:</p> <pre><code>public ConcurrentHashMap&lt;String, WeakReference&lt;Bitmap&gt;&gt; cache=new ConcurrentHashMap&lt;String, WeakReference&lt;Bitmap&gt;&gt;(); 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) &amp;&amp; cache.get(url).get() != null &amp;&amp; !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&lt;WeakReference&lt;Bitmap&gt;&gt; vecBitmapRef = new Vector&lt;WeakReference&lt;Bitmap&gt;&gt;(); if(!cache.isEmpty()) { if(backgroundImageUrl!=null &amp;&amp; !backgroundImageUrl.equalsIgnoreCase("")) { WeakReference&lt;Bitmap&gt; bmpref = (WeakReference&lt;Bitmap&gt;)cache.get(backgroundImageUrl); if(bmpref!=null &amp;&amp; bmpref.get()!=null &amp;&amp; !bmpref.get().isRecycled()) { vecBitmapRef.add(cache.remove(backgroundImageUrl)); } } Set&lt;String&gt; keys = cache.keySet(); for(String key: keys) { vecBitmapRef.add(cache.remove(key)); } Log.e("Bitmap size", ""+vecBitmapRef.size()); try { for(WeakReference&lt;Bitmap&gt; bitmapRef : vecBitmapRef) { if(bitmapRef != null &amp;&amp; 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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload