Java – why doesn’t Android clean up memory after completing the activity?

In my case, I have two activities called a (main activities) and B (children's activities)

If I use Android memory monitor to get memory allocation, I can clearly see the memory allocation increase This is completely normal, because we did some image related things in the activity

So my problem is that if I press the button in activity B, it will enter activity a and the allocated memory will not be cleared Memory allocation will remain the same

Is this normal Android behavior?

If not, how can I manually clean up the memory?

I manually run GC as follows, but the same result has no luck: (

In activity B

@Override
    protected void onDestroy() {
       super.onDestroy();
       System.gc();
    }

Solution

1) Ondestroy is called when finish(); When called, simply press the back button and this method will not be called unless you program it

2)System. gc(); It will not work in ondestroy because your activity B is still active in ondestroy Once the activity B is completely terminated, you should call this method from other activities.

3) If the activity context is leaked to long-standing objects / threads / etc., the activity will remain in memory even if ondestroy is called (memory leak)

Note: call system gc(); Not only does it release your memory, it may not perform GC according to the situation See why is it bad practice to call system gc()?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>