关于内存泄漏和通用方法 [英] About memory leaks and generic methods

查看:85
本文介绍了关于内存泄漏和通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中哪个更适合在我的应用中使用?

Which of these is better to use in my app?

public class NetworkCheck {

    Context context;

    public NetworkCheck(Context context) {
        this.context=context;
    }

    public boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null;
    }
}

...    

if(new NetworkCheck(this).isNetworkConnected()){
    //statement
}

对于上述方法,每次必须使用其方法时,我都必须创建堆内存.当其作用域结束(表示花括号结束)时,其堆内存将被破坏...

For the above one I have to create heap memory every time whenever I have to use its method. Its heap memory will be destroyed when its scope ends (means end of curly braces)...

或者.

public class NetworkCheck {

    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null;
    }
}

...

if(NetworkCheck.isNetworkConnected(){
    //statement
}

为此,我不必创建任何堆内存.我读过许多文章,有人说创建静态变量和方法会导致应用程序中的内存泄漏.

For this one I don't have to create any heap memory. I read many articles where people are saying creating a static variable and method causes a memory leak in the application.

,请帮助我用下面的方法创建此通用的getLocalData() .....

and please help me to c create this Genric getLocalData() of the below method .....

 public static <T> void saveLocalData(Context context, String key, T value) {
        SharedPreferences prefs = context.getSharedPreferences(
                "Qikqrup", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        if (value instanceof String)
            editor.putString(key, (String) value);
        else if (value instanceof Boolean)
            editor.putBoolean(key, (Boolean) value);
        else if (value instanceof Integer)
            editor.putInt(key, (Integer) value);
        editor.commit();
    }

推荐答案

在您的saveLocalData()方法中,不一定存在内存泄漏,但可能存在 内存泄漏,具体取决于您如何使用它.例如,采取以下方法:

In your saveLocalData() method, there isn't necessarily a memory leak, but there can be a memory leak, depending on how you use it. For example, take this:

for( long i = 0;  i < Long.MAX_VALUE;  i++ )
{
    String name = String.valueOf( i );
    saveLocalData( context, name, i );
}

此循环将继续向地图添加值,直到内存用完为止.但是对于任何集合,不仅是地图,都可能发生同样的情况,无论您的集合是否为静态分配,都可能发生这种情况.使用静态集合比使用非静态集合要容易一些,因为据推测,非静态集合可能超出范围并被垃圾回收,而静态集合通常注定会永远存在.

This loop will keep adding values to your map until you run out of memory. But the same can happen with any collection, not just with maps, and it can happen whether your collection is statically allocated or not. With a static collection it is just slightly easier to happen than with a non-static collection, because presumably the non-static collection could conceivably go out of scope and be garbage collected, while the static collection is usually destined to stay forever.

但是,此声明并没有绝对的含义:

However, there is nothing absolute about this statement:

  • 一方面,非静态集合很容易通过被静态对象引用而永久地固定在内存中,而

  • On one hand, a non-static collection may very easily be permanently anchored to memory by being referenced by an object which is static, while:

另一方面,可以由不希望其过大的细心程序员来显式地释放并重新分配静态映射.

On the other hand, a static map may be explicitly freed and reallocated or simply cleared by a careful programmer who does not want it to grow too big.

要编写getLocalData(),您需要声明如下:

In order to write getLocalData() you need to declare it as follows:

public static <T> T getLocalData( Context context, String key, Class<T> classOfValue )

并按如下所示调用它:

String name = MyClass.getLocalData( context, "Name", String.class );

在函数内,您将需要执行以下操作:

within the function, you are going to need to do something like this:

if( classOfValue == String.class )
    return editor.getString( key );
...


您的NetworkCheck的两个版本和您打算的每个版本的用法大致相同,选择一个或另一个之间并没有太多收获.进行new NetworkCheck(this)表示冗余的内存分配,但是正如您已经了解的那样,此内存会很快被垃圾回收,因此不会造成损坏.


Your two versions of NetworkCheck and your intended usage of each is roughly equivalent to each other, there is not much to gain or loose between choosing one or the other. Doing new NetworkCheck(this) represents a redundant memory allocation, but as you already understand, this memory gets garbage-collected very quickly, so there is no damage.

这篇关于关于内存泄漏和通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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