[英]Why android does not clean up memory after finishing activity?
In my case I have tow activities called A(Main Activity) and B (Child Activity). 在我的情况下,我有两个名为A(主要活动)和B(儿童活动)的活动。 A will start B (B having some image loading stuff using Glide image library).
A将开始B(B使用Glide图像库具有一些图像加载内容)。
If I get the memory allocation using android memory monitor, I can clearly see the memory allocation grows.That is totally fine because we are doing some Image related things in activity B.Image Attached. 如果我使用android内存监视器获得内存分配,我可以清楚地看到内存分配增长。这是完全正常的,因为我们在活动中做了一些与图像相关的事情B.Image附加。
So my problem is if I pressed back button in Activity B it will come to activity A and allocated memory will not be cleared.Memory allocation will still in same amount. 所以我的问题是如果我按下活动B中的按钮它将进入活动A并且分配的内存将不会被清除。内存分配仍将保持相同的数量。
Is this normal android behaviour ? 这是正常的Android行为吗?
If not how can I manually clean up memory ? 如果没有,我怎样才能手动清理内存?
I manually run GC as follows but same result no luck :(. 我手动运行GC如下,但同样的结果没有运气:(。
In activity B 在活动B中
@Override
protected void onDestroy() {
super.onDestroy();
System.gc();
}
1) OnDestroy
is called when finish();
1)在
finish();
时调用OnDestroy
finish();
is called, simply pressing back button won't call this method, unless you program it to do so. 被调用,只需按下后退按钮就不会调用此方法,除非您对其进行编程。
2) System.gc();
2)
System.gc();
in onDestroy won't work, since your activity B
is still active at onDestroy
. 在onDestroy中将无法工作,因为您的
activity B
仍在onDestroy
处于活动状态。 You should call this from other Activity once the activity B
is completely terminated. 一旦
activity B
完全终止,您应该从其他活动中调用此方法。
3) Activity will remain on memory even onDestroy
is called, if you leak your activities context to long lived object/thread/etc. 3)如果将活动上下文泄漏到长期存在的对象/线程/等,则即使调用
onDestroy
,活动也将保留在内存中。 (Memory leak) (内存泄漏)
Note: calling System.gc();
注意:调用
System.gc();
does not simply free your memory, it might not execute GC depending on the situation. 不仅仅是释放你的记忆,它可能不会根据情况执行GC。 See details here: Why is it bad practice to call System.gc()?
在这里查看详细信息: 为什么调用System.gc()是不好的做法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.