What are the prospects for Android development

In addition to this, let's define a class in our Activity class, that is Inner class . In order to improve the readability and robustness of the code and encapsulate the program logic, we may do so. What if we create an instance of such an inner class and hold it through static variables? You should be able to guess that this is another memory leak.

void createInnerClass() {
    class InnerClass {
    }
    inner = new InnerClass();
}

View icButton = findViewById(R.id.ic_button);
icButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        createInnerClass();
        nextActivity();
    }
});

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-ddekv7aw-1630589099064)( https://user-gold-cdn.xitu.io/2017/11/13/15fb35700988c536? imageView2/0/w/1280/h/960/ignore-error/1)]

Memory leak caused by internal class

Unfortunately, because the internal class can directly access the variables of its external class, this feature means that the internal class will implicitly hold a reference to its external class, which indirectly leads to our inadvertent disclosure of Activity.

4. Anonymous class

Similarly, an anonymous class holds a reference to the class where it declares. If you are Define and instantiate an AsyncTask anonymous class within the Activity , memory leaks can also occur

void startAsyncTask() {
    new AsyncTask<Void, Void, Void>() {
        @Override protected Void doInBackground(Void... params) {
            while(true);
        }
    }.execute();
}

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View aicButton = findViewById(R.id.at_button);
aicButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        startAsyncTask();
        nextActivity();
    }
});

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-5ugsbzp0-1630589099066)( https://user-gold-cdn.xitu.io/2017/11/13/15fb3570043b3daf?imageView2/0/w/1280/h/960/ignore -error/1)]

Memory leak of AsyncTask

5. Handler

The same principle applies to background tasks: Define an anonymous Runnable and add it to the Handler's processing queue . This Runnable object will implicitly hold a reference to the Activity where it is defined, and then it will be added to the Handler's message queue as a message object. After the end of the Activity life cycle, as long as the message has not been processed by the Activity, there will be a reference chain pointing to our Activity object, so that the Activity object cannot be recycled and leaked.

void createHandler() {
    new Handler() {
        @Override public void handleMessage(Message message) {
            super.handleMessage(message);
        }
    }.postDelayed(new Runnable() {
        @Override public void run() {
            while(true);
        }
    }, Long.MAX_VALUE >> 1);
}


View hButton = findViewById(R.id.h_button);
hButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        createHandler();
        nextActivity();
    }
});

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG lgngymt3-1630589099067)( https://user-gold-cdn.xitu.io/2017/11/13/15fb3570090ee02d?imageView2/0/w/1280/h/960/ignore -error/1)]

Memory leak caused by Handler

6. Thread

For similar problems, we can thread ,Timed task (TimerTask) Found in.

void spawnThread() {
    new Thread() {
        @Override public void run() {
            while(true);
        }
    }.start();
}

View tButton = findViewById(R.id.t_button);
tButton.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
      spawnThread();
      nextActivity();
  }
});

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-b2cb1bsp-1630589099068)( https://user-gold-cdn.xitu.io/2017/11/13/15fb357008f7f2d1? imageView2/0/w/1280/h/960/ignore-error/1)]

Improper use of threads leads to memory leakage

7. Scheduled tasks

As long as they are defined and instantiated through anonymous classes, even if they work in another thread, there will still be a reference chain to the Activity after the Activity is destroy ed, resulting in the disclosure of the Activity.

void scheduleTimer() {
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            while(true);
        }
    }, Long.MAX_VALUE >> 1);
}

View ttButton = findViewById(R.id.tt_button);
ttButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        scheduleTimer();
        nextActivity();
    }
});

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-rrr5crgm-1630589099070)( https://user-gold-cdn.xitu.io/2017/11/13/15fb357038002cf8? imageView2/0/w/1280/h/960/ignore-error/1)]

Memory leak caused by TimerTask

8. System services

Finally, there are some system services that can be accessed through the getSystemService )Method. These services run in their own processes, assist applications in performing some type of background tasks, or interact with the hardware of the device. If the Context object needs to notify the Context when an event occurs in the system service, it needs to treat itself as a monitor Register with system services. The system service thus holds an application to the Activity object. If we forget to de register the listener at the end of the Activity life cycle, a leak will occur.

void registerListener() {
       SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
       Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ALL);
       sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
}

View smButton = findViewById(R.id.sm_button);
smButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        registerListener();
        nextActivity();
    }
});

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-s1pgohop-1630589099071)( https://user-gold-cdn.xitu.io/2017/11/13/15fb357037cc1204? imageView2/0/w/1280/h/960/ignore-error/1)]

Memory leak caused by sensor Manager

last

In a word, students who have no mines at home, if you want to live better in the future, think more about how to arrange your spare time;

The promotion of technology is certainly the top priority, but some "soft power" outside technology can not be completely ignored. Many times, promotion is really because your technology is strong enough, but it is also closely related to your "soft power"

Here, I also share an Android learning PDF + Architecture Video + interview documents + source code Notes collected and sorted by the boss himself, as well as advanced architecture technology brain map, special materials for Android development interview, and advanced advanced architecture materials, which are high-quality materials that I will repeatedly read and learn for my subordinates in my spare time. In the brain map, each knowledge point topic is equipped with corresponding practical projects, which can effectively help you master knowledge points.

In short, it is also here to help you learn and improve, and save you time to search for information on the Internet to learn. You can also share it with your friends to learn together

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code]( https://codechina.csdn.net/m0_60958482/android_p7)**

[external chain picture transferring... (img-Jcwr8tmx-1630589099072)]

[external chain picture transferring... (img-62d9Y9ts-1630589099072)]

Believe in yourself, there is nothing you can't do, only what you can't think of

Tags: Android Database Design Pattern

Posted on Fri, 03 Sep 2021 02:32:46 -0400 by WhiteCube