Does Android Retrofit need to use singletons? do you need to use weakreference to save Retrofit objects in singletons?

whether objects using weakreference in java singletons can be recycled
retrofit is sufficient to use singletons to put retrofit into weakreference

public class RetrofitSingleton {

private WeakReference<Retrofit> retrofitWeakReference;

private RetrofitSingleton() {
}

private static volatile RetrofitSingleton instance = null;

public static RetrofitSingleton getInstance() {
    if (instance == null) {
        synchronized (RetrofitSingleton.class) {
            if (instance == null) {
                instance = new RetrofitSingleton();
            }
        }
    }
    return instance;
}

private synchronized Retrofit getRetrofit() {
    if (retrofitWeakReference == null) {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new TokenHeadweInterceptor())
                .readTimeout(8, TimeUnit.SECONDS)
                .writeTimeout(8, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.URL)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create(GsonUtil.buildGosn()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
        retrofitWeakReference = new WeakReference<>(retrofit);
    }
    return retrofitWeakReference.get();
}

}


will be recycled, regardless of whether it is singleton or not, as long as the object is not referenced by someone else.


depends on what you need to use weakref, to save a strong reference singleton in general. There are not many such singletons under an app


using singleton and weak citation. Paradoxically, you should learn what singleton is and why you use singleton


.
  1. whether objects using weakreference in java singletons can be recycled
the existence of a weak reference object does not prevent the object it points to from being reclaimed by the garbage collector. The most common use of weak references is to implement canonical mapping (canonicalizing mappings, such as hash tables)

in a nutshell, an object can be recycled when it has only a weak reference to it.

2.retrofit is sufficient to use singletons to put retrofit into weakreference.
Yes, but not necessary. In addition, there is also a problem with the non-empty judgment of the code, which does not determine whether the object has been reclaimed.

private synchronized Retrofit getRetrofit() {
    if (retrofitWeakReference == null) {
        *******
        retrofitWeakReference = new WeakReference<>(retrofit);
    }
    return retrofitWeakReference.get();
}

I just said that the object referenced by WeakReference will be recycled, not the WeakReference object itself, that is, retrofitWeakReference.get () will return null.. So we should judge

    if (retrofitWeakReference == null || retrofitWeakReference.get() == null) {
       ******
    }

actually, it's not necessary to use WeakReference, normally here because you want this instance to exist all the time (so use a singleton). Not only is it used here, but WeakReference is also used incorrectly.


any object referenced by weakreference will be reclaimed by the garbage collection period after the end of the method. But for the general retrofit, since you want to hold simple interest for a long time, and think of a way to recycle it after the end, is it not good to do so?
looks like your intention in the Ming Dynasty. Do you want to do some network requests for memory leaks? You can combine application and baseActivity to do

if you are interested in android technology, you can follow Wechat's official account: terminal R & D Department, and communicate and study with me.

reference reading:

Technical articles Collection-184articles classified

Why do you work so hard but never get a promotion?

Terminal R & D Department is a technology-based learning and exchange technology number, talking about technology, products, but also our lives. Be the most thoughtful and interesting Internet developer in the Eastern Hemisphere

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b310aa-2bd48.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b310aa-2bd48.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?