0

My app current has some memory issues, and one of them is this; when I switch between two seemingly simple Activities (each has ~30 TextViews and a few buttons), the Allocated Memory in Android Profile just keeps on increasing. Allocated Memory starts off at about 10MB, and after switching about 20 times it's at 200MB.

I'm calling finish() during onStop of each Activity. Also, onDestroy is getting called each switch.

I've had a thorough look for memory leaks, and don't think it's my code (famous last words?).

Any thoughts on what might be causing it? Is Android Profiler's Allocated Memory reading accurate enough to worry about?

I've no issues running my app on a few physical devices.

EDIT:

I have now tested this with 2 simple Activities. They are identical, apart from button text and where the intent goes, as seen here;

public class ZZZ1Activity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zzz1);
}

public void SwitchClick(View v)
{
    Intent intent = new Intent(this, ZZZ2Activity.class);
    startActivity(intent);
}
}

and

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ZZZ2Activity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="SwitchClick"
    android:text="SWITCH to 1"
    />

</android.support.constraint.ConstraintLayout>

Switching between these two Activities produces the same result; my Allocated Memory skyrockets. Slower than my real activities, but still alarmingly quick. 100mb+ in 20 switches.

ausgeorge
  • 975
  • 1
  • 12
  • 23

2 Answers2

0

An activities reserved memory may still be held in the memory until the garbage collector removed it. That might be the reason for your issue. Refer to this post for further info.

Jujinko
  • 319
  • 3
  • 21
0

I found the issue. I have a Fragment with a UI/View. When an Activity calls onDestroy, it calls the Fragment's onDestroyView. In this I have to clear all references to any UI elements, like this;

@Override
public void onDestroyView()
{
    super.onDestroyView();
    ClearMemory();
}

private void ClearMemory()
{
// clear all references to Views
    btn_newgame = null;
} 
ausgeorge
  • 975
  • 1
  • 12
  • 23