简体   繁体   English

防止Android中的内存泄漏

[英]Preventing memory leaks in Android

Is it wise to get a reference to a Context object in every Activity where I need a Context by getting the Application context? 通过获取应用程序上下文在我需要上下文的每个Activity中获得对上下文对象的引用是否明智? I have learned that it can create memory leaks to throw around your Activity's context object but when you create complex Activities it seems that a Context object is almost always necessary. 我了解到,它可能会导致内存泄漏,从而使您的Activity的上下文对象泛滥成灾,但是当您创建复杂的Activity时,似乎几乎总是需要Context对象。 I was previously declaring a Context variable at the top of the Activity class and initializing it with the "this" keyword in onCreate . 之前,我在Activity类的顶部声明了一个Context变量,并在onCreate使用“ this”关键字对其进行了初始化。 I already know this can be poor form, but is it ok to initialize the Context object in onCreate calling getApplicationContext() ? 我已经知道这可能是糟糕的形式,但是可以在调用getApplicationContext() onCreate初始化Context对象吗? In other words does this help solve my problem. 换句话说,这有助于解决我的问题。

Also, is it better practice to limit the use of static variables? 此外,限制静态变量的使用是否是更好的做法? If I'm not mistaken, if I call a static method, or reference a static variable from a different Activity, won't that keep the other Activity in memory too? 如果我没记错的话,如果我调用一个静态方法,或者从另一个Activity引用一个静态变量,那是否也将另一个Activity保留在内存中?

  1. There is really no need of a Context field in your Activity , as you can always get the context using getBaseContext() , getApplicationContext() , or this (since Activity itself is Context). 实际上,您的Activity不需要Context字段,因为您始终可以使用getBaseContext()getApplicationContext()this (因为Activity本身是Context)来获取上下文。

  2. You might have to pass your Context to other classes if you want to keep you Activity class thin. 如果要使Activity类保持精简,则可能必须将Context传递给其他类。 This is perfectly ok as long as the lifecycle of those classes is the same as the lifecycle of your Activity . 只要这些类的生命周期与Activity的生命周期相同就可以了。 This means, when your Activity is destroyed, no objects should have the reference to the context you passed. 这意味着,当您的活动被销毁时,任何对象都不应引用您传递的上下文。

  3. Static methods are extremely good as long as they don't refer to static fields. 静态方法非常好,只要它们不引用静态字段即可。 Use static methods if they don't have a state. 如果它们没有状态,请使用静态方法。 Static fields are dangerous for a lot of reasons. 静态字段很危险,原因有很多。 So use them only for the right scenarios. 因此,仅在正确的情况下使用它们。

I think you need to understand the differences between Application Context and Activity Context, please refer to the answer here . 我认为您需要了解Application Context和Activity Context之间的区别,请参阅此处的答案

But is it ok to initialise the Context object in onCreate calling getApplicationContext()? 但是可以在调用getApplicationContext()的onCreate中初始化Context对象吗? In other words does this help solve my problem. 换句话说,这有助于解决我的问题。

Why do you need to initialise a context object? 为什么需要初始化上下文对象? Activity itself is already a context. 活动本身已经是一个上下文。 For example: 例如:

    Intent intent = new Intent(this, MainActivity.class);

You don't need to initialise a context from application context as the activity context has more capabilities. 您不需要从应用程序上下文中初始化上下文,因为活动上下文具有更多功能。 Please refer to this link . 请参考此链接

Also, is it better practice to limit the use of static variables? 此外,限制静态变量的使用是否是更好的做法? If I'm not mistaken, if I call a static method, or reference a static variable from a different Activity, won't that keep the other Activity in memory too? 如果我没记错的话,如果我调用一个静态方法,或者从另一个Activity引用一个静态变量,那是否也将另一个Activity保留在内存中?

For sending a data from one activity to another activity, you might need parcelable object and bundle , or Event Bus to decouple both sender/receiver activity. 为了将数据从一个活动发送到另一个活动,您可能需要可打包对象和bundle事件总线来分离发送者/接收者活动。

For static method, you might need to group them under a Utility class. 对于静态方法,您可能需要将它们分组在Utility类下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM