Activity中的成员变量被赋值之后,Actiity被回收的时候内存才会被释放吗
java中一个类中的成员变量被赋值之后,这个类对象被回收时,类中得成员变量才会被释放内存吗
大家讲道理2017-04-18 10:51:07
This question should be classified under Java. As long as it is Activity
所属的对象被static持有了, 那就导致整个Activity
一直存在在内存中. 除非使用WearReference
.
The following is a similar error code demonstration:
private static Context sContext;
public static void setContext(Context context) {
sContext = context;
}
If you use Android Studio, the IDE will have a warning: Do not assign the Context object to static variables.
伊谢尔伦2017-04-18 10:51:07
The description of the question is unclear, and there are no negative comments on the punctuation. But try to answer:
Global objects should only be variables modified by the static modifier. Variables after static modification belong to this class (class), or change the class corresponding to All objects, not belonging to a certain object. From this derivation, you should be able to realize that it is wrong for an object to be recycled to cause static to be recycled (how to use other objects after it is recycled)
Here we talk about Activity, let’s expand on it. Under normal circumstances, it is okay to use static variables to reference the current object (such as singleton mode), but in the design of Android, Activity is not an ordinary class. It has its own life cycle and will be recycled when it expires (because the function is too powerful , occupying too much memory). So there is a problem of using static reference to the current Activity to report a memory leak.