How to clear dynamically created view from memory?

给你一囗甜甜゛ 提交于 2019-12-05 08:17:15

Typically, you don't need to worry about clearing views from memory. You would let the virtual machine and Android framework handle this when it is necessary. However, you do need to be concerned with memory leaks. If your Activities are sharing/holding onto references to views, and they cannot be garbage collected, then that is a problem. You can start by reading about that here: http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

Its hard to provide some more specific advice without seeing you code though...

In onDestroy() set the views to null and call the garbage collector.

@Override
public void onDestroy() {
    super.onDestroy();
    myView = null;
    System.gc();
}

This can help the garbage collector by calling System.gc() but it isn't guaranteed that the memory is cleared. However as long as you don't have a leak, there shouldn't be a problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!