Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding memory management when navigating inside Activities
    primarykey
    data
    text
    <p>I have setup a little example application where the idea is to navigate from one Activity to another and to study Memory consumption because I don't really understand when/where memory is released during this process. </p> <p>Idea is to create an Activity which consume quite a lot of memory to see if memory is released correctly when we leave it before recreating it.</p> <ul> <li>A HomeActivity only composed of a Button that call a BlogListActivity when button is clicked.</li> <li>BlogListActivity is a ListActivity that contain BlogPost objects. This BlogPost contains a Bitmap in order to use some memory.</li> </ul> <p>List of BlogPost is created dynamically in the onCreate method of BlogListActivity and then passed to an Adapter to display each PostBlog object in row of my ListView.</p> <p>On an emulator with Android 2.3.3 and 128Mo of memory, I manage to move from HomeActivity to BlogListActivity and then come back to HomeActivity two times. On the third try, I get an OutOfMemoryError from BitmapFactory.</p> <p>This mean I have a Memory Leak: objects that are not used anymore but still have a reference on it so they are not released. But I don't where I do it wrong.</p> <p>Can someone help me finding it.</p> <p>Thanks in advance for your help.</p> <p>Bertrand</p> <p><a href="http://dl.dropbox.com/u/92487/WebContentListView.zip" rel="nofollow noreferrer">Link to complete source code and Eclipse project</a></p> <p>Here is an extract of the code we are interested in</p> <p>HomeActivity source code</p> <pre><code>public class HomeActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); } public void onSecondActivityClick(View v) { startActivity(new Intent(this, BlogListActivity.class)); } } </code></pre> <p>BlogListActivity source code</p> <pre><code>public class BlogListActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bloglist); List&lt;BlogPost&gt; items = new ArrayList&lt;BlogPost&gt;(); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mn); for (int i = 0; i &lt; 5; i++) { BlogPost post = new BlogPost(); post.author = String.format("Author%d", i); post.title = String.format("Title%d", i); post.date = new Date(); post.imageURL = "https://si3.twimg.com/profile_images/1143791319/MN_BLEU.png"; post.image = bmp; post.image = BitmapFactory.decodeResource(getResources(), R.drawable.mn); items.add(post); } setListAdapter(new LazyArrayAdapter(this, R.layout.listitem_blog, items)); } </code></pre> <p>}</p> <p>LazyArrayAdapter source code</p> <pre><code>public class LazyArrayAdapter extends ArrayAdapter&lt;BlogPost&gt; { public LazyArrayAdapter(Context context, int textViewResourceId, List&lt;BlogPost&gt; objects) { super(context, textViewResourceId, objects); } @Override public View getView(int index, View view, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); if (view == null) { view = inflater.inflate(R.layout.listitem_blog, parent, false); } TextView title = (TextView)view.findViewById(R.id.listitemblog_title); TextView date = (TextView)view.findViewById(R.id.listitemblog_date); ImageView icon = (ImageView)view.findViewById(R.id.listitemblog_icon); BlogPost post = this.getItem(index); title.setText(post.title); date.setText(new SimpleDateFormat().format(post.date)); icon.setImageBitmap(post.image); return view; } } </code></pre> <p>BlogPost source code</p> <pre><code>public class BlogPost { public String title; public String author; public Date date; public String imageURL; public Bitmap image; } </code></pre> <p>activity_bloglist Layout</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/list"&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>ListItemBlog Layout</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:weightSum="100" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:layout_width="0px" android:layout_weight="70" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_height="fill_parent"&gt; &lt;TextView android:id="@+id/listitemblog_title" android:layout_width="wrap_content" android:text="TextView" android:textStyle="bold" android:layout_height="wrap_content"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/listitemblog_date" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:textStyle="bold"&gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; &lt;ImageView android:id="@+id/listitemblog_icon" android:layout_width="0px" android:scaleType="centerInside" android:layout_weight="30" android:src="@drawable/icon" android:layout_height="fill_parent"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>HomeActivity layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;Button android:layout_height="wrap_content" android:onClick="onSecondActivityClick" android:layout_width="wrap_content" android:id="@+id/button1" android:text="Button"&gt; &lt;/Button&gt; &lt;/LinearLayout&gt; </code></pre> <p>I have studied memory usage with DDMS + MAT. Here are screenshots of what I see in MAT for the com.webcontentlistview I create: Memory usage after navigating to BlogListActivity one time <img src="https://i.stack.imgur.com/rtaFk.png" alt="Memory usage after navigating to BlogListActivity one time"></p> <p>Memory usage after navigatin to BlogListActivity several times <img src="https://i.stack.imgur.com/QRJQO.png" alt="Memory usage after navigatin to BlogListActivity several times"></p> <p>As we can see, even after navigating between both Activity, we still have only one BlogListActivity object in memory (with it's associated content). But numbers of java and android objects are increasing (lines 2 and 3).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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