JcDenton86
JcDenton86

Reputation: 932

Android memory usage

i am building an android application but i have some questions about the memory usage.

Most of the data i need and use are string arrays stored in the xml strings file. I used arrays because firstly the biggest array will have up to 30 items and secondly there won't be any updating or deleting or inserting items through the app. All the custom adapters i created are following googles' guidlines (the fast way - using the holder class)

As the user switches between the activities, depending on the selections he makes different arrays are loaded in the listviews.

Does android clears the memory each array allocates if its not in use? should i do that? I ve used MAT also to check how the app uses memory and to check for leaks etc..and i thing that everything is fine. I also use a few png icons/images. The app gets 5MB when it starts, goes up and down up to 8.5-9MB as the user plays around.

Thanks in advance for any help!

Upvotes: 0

Views: 243

Answers (1)

triad
triad

Reputation: 21507

It's possible the Android OS will kill your Activities (without focus) on the stack if memory is needed. When your Activity is killed in this way, onSaveInstanceState(Bundle outState) is called. You should save your string array here.

When onCreate(Bundle savedInstanceState) is called in your Activity, if savedInstanceState is not NULL, then it means your Activity was previously killed by the OS and you need to repopulate your string array from that bundle.

ex:

String [] stringArray;
...

protected void onCreate(Bundle savedInstanceState)
{
    if (savedInstanceState != null)
    {
        stringArray = savedInstanceState.getStringArray("some_key");
    }
}

protected void onSaveInstanceState (Bundle outState)
{
    super.onSaveInstanceState(outState);
    outState.putStringArray("some_key", stringArray);
}

This is described in more detail here: http://developer.android.com/reference/android/app/Activity.html

Upvotes: 1

Related Questions