可以通过上下文的静态helper方法作为参数,可能会导致内存泄漏? [英] Could passing Context to a static helper method as an argument potentially cause a memory leak?

查看:255
本文介绍了可以通过上下文的静态helper方法作为参数,可能会导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种静态方法辅助类,这些方法之一,需要上下文访问的一些信息。可以通过上下文的静态helper方法作为参数,可能会导致内存泄漏?

I have a helper class that consists of various static methods, one of those methods requires Context to access some information. Could passing Context to a static helper method as an argument potentially cause a memory leak?

public class ConnectionHelper {
    public static boolean isConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm != null;
    }
}

如何静态方法的行为,它已经执行后处置上下文引用的还是会在内存中后流连?

How does the static method behave, does it dispose of the context reference after it has executed or will it hang around in memory afterward?

推荐答案

取决于你在通过了上下文引用做什么。

Depends on what you do with the Context reference passed in.

如果您存储参考无限期,它会泄漏。没有什么做的静态 -ness。 (虽然不小心存放时间过长的引用很容易与静态的变量的。)

If you store the reference indefinitely, it will leak. Nothing really to do with static-ness. (Though accidentally storing references for too long is easy with static variables.)

您发布不随地存储方法的范围参考值,code不漏。

The code you posted does not store references anywhere outside the method's scope and does not leak.

这篇关于可以通过上下文的静态helper方法作为参数,可能会导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆