Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android memory test

This may be a stupid question but i can't seem to find alot of specific information on the internet.

Lets say i have 2 activities: MainActivity & Secondactivity. The mainactivity has a button to go to the secondactivity. The second activity has a button that goes back to the main activity (very simple code below).

I am trying to understand android memory management and that's why i'm doing this test.

My question:

When i constantly go back and forth between the activities, while looking at the memory graph in Android studio, i can see a blue graph that never goes back down to it's allocated memory that it had on startup of the application. Do i have a memory leak? (Probably not cause this is basic code). But then why does it never go back to it's original allocated memory at the beginning?

The mainactivity has only this method:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnTest = (Button) findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Log.i(null, "test");
            finish();
            startActivity(new Intent(MainActivity.this, SecondActivity.class));
        }
    });
}

While the SecondAcitivity just goes back to the first one and creates a few Buttons

public class SecondActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    Button btnTest = (Button) findViewById(R.id.btnTest);
    btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Log.i(null, "test");
            finish();
            startActivity(new Intent(SecondActivity.this, MainActivity.class));
        }
    });

}

}

enter image description here

like image 711
DennisVA Avatar asked Nov 26 '22 04:11

DennisVA


1 Answers

I think the problem was using genymotion as an emulator. You should use a real device for memory analysis, also the older memory monitor is more precise.

like image 90
DennisVA Avatar answered Dec 05 '22 03:12

DennisVA