Activity memory leak caused by anonymous threads
An anonymous inner class will hold a reference to the this pointer of the outer class and will not be released until the thread ends.
We have detected that your implementation has a lot of reading and writing data in anonymous threads of the Activity, and these threads will hold the Activity and prevent its timely release.
The most involved API is RetrofitPeertubeAPI(Context context). In fact, the input parameter only needs the context type and not the Activity type, so we can replace the passing of the activity object with the ApplicationContext with a longer life cycle.
You can keep a static reference to the ApplicationContext for RetrofitPeertubeAPI. At the same time, new Thread(v->(RetrofitPeertubeAPI task) can be encapsulated into a public static class to eliminate unnecessary references.
class RetrofitPeertubeAPI{
Context _context = Application.getApplicationContext();
}
public RetrofitThread extends Thread{
public void run(){
...//RetrofitPeertubeAPI task
}
}
This has happened about 70 times, and it's not expensive to fix them but very rewarding.