I. Causes of Memory Leakage
1 Singleton Reference context
The context of each activity is the same as that of the activity. When the activity is released or killed, its context will be released. However, the singleton in the current activity still exists, resulting in the context still being referenced and can not be garbage collected, leading to memory leaks.
2 Memory leak caused by unregistered registration
Some Android programs may refer to objects of our Anroid program (such as registration mechanisms). Even though our Android program is over, other reference programs still have references to an object of our Android program, which cannot be garbage collected, leading to memory leaks.
3 Memory leaks caused by creating static instances of non-static internal classes
The life cycle of non-static internal classes is the same as that of current classes, while the static cumulative life cycle is the same as that of the whole app. The end of the current class does not kill the variables of static, resulting in the reference of non-static classes still exists and can not be garbage collected, leading to memory leaks.
4 Initiated by Thread Class
Open self-threading in activity. When activity ends, the sub-threads will not end. If there are still references to external variables in the sub-threads, memory leaks will also occur.
5 Caused by handler
Two Memory Leakage Analysis
First we create an activity with memory leaks
public class MainActivity2 extends Activity {
private SingleInstance singleInstance;
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
private static TextClass textClass;
private TextView text_tv;
//Memory leak 4 Memory leak due to handler
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_tv= (TextView) findViewById(R.id.text_tv);
text_tv.setText("aaa");
//Memory leak 1 singleton reference context
singleInstance=SingleInstance.getInstance(this);
//Memory Leak 2 Registered Broadcast Not Closed Open Cursor Not Closed
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //Add the broadcast you want to receive
networkChangeReceiver = new NetworkChangeReceiver(); //Broadcasting examples
registerReceiver(networkChangeReceiver, intentFilter);
//Memory leak 3 Memory leak caused by creating static instances of non-static internal classes
if(textClass==null){
textClass=new TextClass();
}
//Memory leak 5 is triggered by thread classes
new Thread(){
@Override
public void run() {
super.run();
String s=text_tv.getText().toString();
}
}.start();
handler.sendEmptyMessage(111);
}
class TextClass{
}
Open this interface and return to the previous interface
Observing memory Fluctuation with studio
adopt To start looking for memory overflow in this interface
Waiting for a while before appearing
Click on the top right corner Current specific analysis will emerge.
Find the problem and click jump to Source to jump to the problem class
Three Use MAT Specific Analysis
MAT Tool Download Address:: https://www.eclipse.org/mat/
(1) Export standard hprof files
(2) Use MAT to open the export file
(3) Use Enter select * from instance of android. app. Activity to query if the activity is leaked by memory
The results of the query are as follows
Sure enough, MainActivity 2, which has been shut down by us, still appears here, and then we can check if there is a memory leak.
Open and appear
If there is a problem with textClass, we can correct it correctly.