用于静态声明上下文和INSTANCE的内存泄漏,如何更改它?

用户名

当前在我的代码库中,我有以下类(其中的一部分),其中向我显示2次内存泄漏,并显示消息“请勿将Android上下文类放在静态字段中(对Myclass的静态引用,其中MyContext的字段上下文指向Context);这是内存泄漏(并且还会中断即时运行)”,我不确定是什么替代方案。这是100%的内存泄漏吗?我收到关于“ INSTANCE;”的泄漏警告;和针对上下文的“静态”声明。任何想法如何去解决它?

public enum Myclass {
        INSTANCE;

    public static final boolean TLS_ENABLED = true;
    private static final String TAG = Myclass.class.getSimpleName();

    private static final String SP = "My_class";


    private static Context context;
       public void init(Context context, String appKey, String appSecret) {
        init(context, null, appKey, appSecret);
    }

    /**
     * Initialize class
     *
     * @param context   Application level context.
     * @param apiUrl    API url of backend server
     * @param appKey    Application key
     * @param appSecret Application secret
     * @throws IllegalArgumentException If activity instance will be passed as the context
     * @throws IllegalArgumentException If application key is empty or null
     * @throws IllegalArgumentException If application secret is empty or null
     */
    public void init(Context context, String apiUrl, String appKey, String appSecret) {
        if (null == context) { throw new NullPointerException(); }

        if (!(context instanceof Application)) { throw new IllegalArgumentException("Supply my class with application context"); }

//        if (TextUtils.isEmpty(apiUrl)) { throw new IllegalArgumentException("Api url can't be null or empty string"); }

        if (TextUtils.isEmpty(appKey)) { throw new IllegalArgumentException("App key can't be null or empty string"); }

        if (TextUtils.isEmpty(appSecret)) { throw new IllegalArgumentException("App secret can't be null or empty string"); }

        this.apiUrl = apiUrl;
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.sp = context.getSharedPreferences(SP, Context.MODE_PRIVATE);
        MyClass.context = context;
        initManagers();
    }

    /**
     * Initializes managers. This method must be called after constructor
     * returns, as the managers during own initialization may use myclass.get()
     * method.
     */
    private void initManagers() {
        accountManager = new AccountManager();
        myclassApi = new MyclassApi(context, apiUrl);
        contactManager = new ContactManager();
        connectionManager = new ConnectionManager();
        meetingListManager = new MeetingListManager();
    }


    /**
     * Returns {@link Context} that was passed to
     * {@link myclass#init(Context, String, String)}.
     *
     * @return
     */
    public static Context getContext() {
        return context;
    }

    /**
     * Returns {@link SharedPreferences} instance.
     *
     * @return SharedPreferences
     */
    public SharedPreferences getSp() {
        return this.sp;
    }


       public static class Event<T> {
        private State state = State.SUCCESS;
        private Throwable t;
        private T data;
        private String errorMessage;

        /**
         * Event state. If event related to network request/response
         * operations - state indicates the physical (not logical)
         * success or fail of request.
         */
        public enum State {
            /**
             * Indicates that attempt to get data or perform task successful
             */
            SUCCESS,
            /**
             * Indicates that attempt to get data or perform task fails,
             * and reason of fail is the incorrect request data
             */
            FAIL,
            /**
             * Indicates that attempt to get data or perform task encounter an error
             * mostly due to connection problem
             */
            ERROR,
            /**
             * Indicates that attempt to get data or perform task was ignored
             * according to internal state of event producer.
             */
            IGNORED
        }


}
Elmorabea

它的安全在一个静态字段存储应用环境中,可以简单的调用context.getApplicationContext()上的任何上下文引用您将其存储在一个静态字段之前获得。

无论如何,应用程序上下文都是单例,您不能泄漏它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在静态上下文中使用带有上下文参数的类而不引起内存泄漏?

如何防止静态字段中的上下文类导致的内存泄漏

不要将Android上下文类放在静态字段中;这是内存泄漏

内存泄漏,但是我如何传递与解决泄漏的活动不同的上下文?

Android:由于活动和上下文存储为对象属性而导致内存泄漏?

带有泄漏上下文的静态字段

具有上下文的无内存泄漏的Singleton

我的PyV8上下文泄漏内存

Android:使用共享 EGL 上下文时内存泄漏

将活动上下文传递到静态方法中,是否可能发生内存泄漏?

应该如何声明上下文?

在 Ionic React 中的页面更改后,如何更改上下文的值而不重置它?

如何更改lambda的上下文?

如何解决SpriteKit SKTexture的CGImage“上下文泄漏”?

如何使用声明性管道更改多分支项目上的上下文消息?

警告:请勿将Android上下文类放在静态字段中;这是内存泄漏(并且还会中断即时运行)

“警告:不要把Android的上下文类的静态字段;这是一个内存泄漏(也打破了即时运行)”

并发静态上下文

如何分离编辑和更新上下文(静态管理,点表示法)

在Thymeleaf中禁用静态上下文和方法调用

如何从静态上下文中获取资源内容?

如何从静态上下文中获取getclass()。getResource()?

如何获取调用静态函数的活动的上下文?

Laravel如何在静态方法中使用$ this上下文?

如何从非活动的静态上下文中引用“getSharedPreferences”

可以从Android中的Singleton中释放上下文,以防止内存泄漏

Android - 应用程序上下文可以解决内存泄漏吗?

具有活动上下文的处理程序类会导致内存泄漏吗?

我在泄漏上下文吗?