匿名 class 对象是否应该设置为 null onDestroy?

Should anonymous class object set to null onDestroy?

听说the Anonymous Classes can leak memory

Similarly, Anonymous Classes will also maintain a reference to the class that they were declared inside. Therefore a leak can occur if you declare and instantiate an AsyncTask anonymously inside your Activity. If it continues to perform background work after the Activity has been destroyed, the reference to the Activity will persist and it won’t be garbage collected until after the background task completes.

是否应将匿名 class 对象设置为 null onDestroy 以防止内存泄漏?这是我的一些代码。

public class RegisterActivity extends AppCompatActivity {
    private ApiHandler registerHandler = null;
    private static final int SERVICE_REQUEST_REGISTER = 243;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        useApiService();
        initApiHandler();
    }

    protected void useApiService() {
        apiService = ApiClient.getClient(getApplicationContext()).create(ApiInterface.class);
    }

    private void initApiHandler() {
        registerHandler = new ApiHandler(this, SERVICE_REQUEST_REGISTER) {
            @Override
            protected String successStatusCode() {
                return "802";
            }

            @Override
            protected String secretKey() {
                return getDefaultKey();
            }

            @Override
            protected boolean isExchangeSecretKey() {
                return false;
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        registerHandler = null;
    }
}

实际上垃圾收集器会为您完成。

你粘贴的这段文字说的是,如果你正在创建的匿名 class 启动一个新的 AsyncTask,你创建它的主要 class 将永远不会被销毁...

换句话说,当有一个匿名 class 运行 任务 onDestroy 永远不会在 main class

上调用