instantiation

An object is an instance of a class. The process of creating an object is also called instantiation of a class. Objects are created using classes as templates. For example, Car Car = new Car(); , we Create a new class instance of Car.

Directed graph

A graph marked with A directed line segment on each edge is called A directed graph. In Java, garbage Collection adopts the way of directed graph for memory management, and the direction of the arrow represents the reference relationship. For example, B ← A requires A in B, and B references A.

Shallow heap

Represents the memory consumed by the current object

The garbage collection

Java Garbage Collection (GC) is one of the main differences between Java and C++/C. As Java developers, they generally do not need to write special memory Collection and Garbage cleaning code, and they do not need to be afraid of memory leakage and overflow problems like C programmers. This is because there are automatic memory management and garbage cleaning mechanisms in Java virtual machines. Generally speaking, this mechanism marks the memory in a VM and determines which memory needs to be reclaimed. According to a certain reclamation policy, the system automatically reclaims the memory to ensure that the memory space in the VM is Nerver stopped to prevent memory leaks and overflow.

What is a memory leak

When you no longer need an instance, the object is still referenced, preventing from being bargage collected. This situation is called a Memory Leak.

In Android, memory leaks are often caused by different life cycles. Here are a few common reasons:

Static variables cause memory leaks

public class SecondActivity extends AppCompatActivity{ private static Context sContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); sContext = this; findViewById(R.id.finish).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); }}); }}Copy the code

Memory leaks are also possible because static variables hold the current Activity. Causes the static variable to still hold its reference when the current Activity ends. A deeper dive into the life cycle of static variables in Android is necessary. See the Life Cycle of Android Static Variables. This article is very clear and highly recommended.

Non-static inner classes hold instances of external classes

public class ThirdActivity extends AppCompatActivity {

  private static MySample sMySample=null;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    if(sMySample==null){
      sMySample=new MySample();
    }
  }
  class MySample{

  }
}
Copy the code

The code above results from LeakCanary:

Because we create a singleton of a non-static inner class inside the Activity and use that singleton’s data every time we start the Activity (to avoid duplicate resource creation), this writing causes a memory leak, again because the non-static inner class holds an external class object. Instead, make the inner class static or extract it into a singleton. If Context is required, use ApplicationContext.

Memory leak caused by thread

Runnable is an anonymous inner class (if AsyncTask has an anonymous inner class) that has an implicit reference to the current Activity. If the task is not completed before the current Activity is destroyed, the Activity’s memory resources cannot be reclaimed, resulting in a memory leak. Example code is as follows:

public class FourthActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fourth); sample(); finish(); } private void sample() { new MyThread().start(); } private class MyThread extends Thread { @Override public void run() { while (true) { SystemClock.sleep(1000); }}}}Copy the code

Not surprisingly, we soon received notification of a memory leak. To do this, use a static inner class, as shown below:

private static class MyThread extends Thread { @Override public void run() { while (true) { SystemClock.sleep(1000); }}}Copy the code

Memory leak caused by Dialog

It is also easy to leak memory by not demolish the current Dialgo before the Activity to which the current Dialog is attached is destroyed.