Android memory test

This may be a stupid question, but I cannot find much specific information on the Internet.

Suppose I have 2 actions: MainActivity and Secondactivity. The main activity has a button to go to the second action. The second action has a button that goes back to the main activity (very simple code below).

I am trying to understand Android memory management and why I am doing this test.

My question is:

When I constantly go back and forth between actions, looking at the memory graph in the Android studio, I see a blue graph that never returns to the allocated memory that it had when the application started. Do I have a memory leak? (Perhaps this is not the case as the main code). But why does it never return to the original allocated memory at the beginning?

The main activity 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 SecondAcitivity just goes back to the first 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

+6
source share
4 answers

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

+1
source

There is no memory leak in your code. The reason for the increase in allocated memory is due to the fact that your application will use some to translate into different types of activities. But, if you keep running this code forever, you will see sudden crashes, I did this:

enter image description here

Whenever garbage collection occurs, the allocated memory is omitted. If, however, you have a memory leak, this means that the GC will not be able to get rid of certain objects. To do this, you will see a trend line with a general increase until a memory error occurs.

How can you check this?

I suggest you make intentions invoked onCreate for an endless loop of back-and-forth operations. If you do not get an OutOfMemory error, you probably have no memory leak.

In the developer's guide:

As you select more objects in your application, you will force periodic garbage collection, creating small β€œhiccups” in the user interface. The compatible garbage collector introduced in Android 2.3 helps, but unnecessary work should always be avoided.

0
source

The blue graph is the amount of allocated memory.

Why allocated memory is increasing

When you switch between two actions, your application is allocated memory to create new actions.

Why the allocated memory does not decrease

This does not diminish because memories are released during garbage collection. Dalvikvm will determine when garbage collection will occur, so garbage collection may not occur for a long period of time. You can continue to move between the two actions to increase the allocated memory, and as soon as the allocated memory is large enough, you should see a sharp drop in the amount of allocated memory.

Why does a sudden drop in allocated memory occur

The allocated memory is freed during garbage collection. If you look at logcat in android studio, a sudden drop in allocated memory should be accompanied by a message issued by Dalvikvm GC_something. This means that garbage collection has occurred and memories have been released. You can observe this by clicking the GC trigger button (to the left of the blue graph) to manually start garbage collection.

-1
source

It is simply a matter of garbage collection (GC). Even if you write minimal code, a structure can do a lot of work behind the scenes. You actually explicitly create multiple objects on every click.

Here is a memory graph where you can see the GC effect. enter image description here enter image description here

-1
source

All Articles