http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html
The "static" keyword has different meanings when it comes to "static variables" vs. "static classes" in Java.
A static variable is a variable that belongs to all instances of a particular class. It will not be reclaimed by the GC when a particular instance of the class is garbage collected... it will stay in memory until you explicitly set it to null. The reason why it is bad to hold a static reference to a Drawable is because a Drawable usually holds a reference to a View and that View usually holds a reference to its parent Activity. As a result, the static Drawable will force the Activity to stay in memory even after the Activity is destroyed (unless you explicitly set the static Drawable to null, of course).
Static classes in Java don't really have the same meaning as static variables in Java. A static class declaration gives you a way to declare an inner class as if it was declared in a separate .java file... and that's pretty much it. A non-static inner class on the other hand is implicitly associated with its outer class... unlike static inner classes, an instance of a non-static inner class cannot exist without an instance of its outer class as well.
To answer your second question, declaring the mHandler as static would not have the same effect as the above sample code. Making the handler static means that it would be shared by all instances of the Activity (i.e. if you rotated the screen causing the Activity to be destroyed, the same Handler object would be used by the newly created Activity instance as well). In the sample code above, the Handler is declared as non-static which means that a new Handler will be created for each new Activity that is instantiated.
Yes, the Runnable is static so a single instance will be allocated and will be shared across all Activity instances (a new one will not be created for each new Activity that is created). I explain why it is important for the Runnable to be static in the second to last paragraph of the post.
java static and non-static class and variable
https://drive.google.com/file/d/0B-5-QeQdT3FxQUdnUWNFTUMxdXc/view
The "static" keyword has different meanings when it comes to "static variables" vs. "static classes" in Java.
A static variable is a variable that belongs to all instances of a particular class. It will not be reclaimed by the GC when a particular instance of the class is garbage collected... it will stay in memory until you explicitly set it to null. The reason why it is bad to hold a static reference to a Drawable is because a Drawable usually holds a reference to a View and that View usually holds a reference to its parent Activity. As a result, the static Drawable will force the Activity to stay in memory even after the Activity is destroyed (unless you explicitly set the static Drawable to null, of course).
Static classes in Java don't really have the same meaning as static variables in Java. A static class declaration gives you a way to declare an inner class as if it was declared in a separate .java file... and that's pretty much it. A non-static inner class on the other hand is implicitly associated with its outer class... unlike static inner classes, an instance of a non-static inner class cannot exist without an instance of its outer class as well.
To answer your second question, declaring the mHandler as static would not have the same effect as the above sample code. Making the handler static means that it would be shared by all instances of the Activity (i.e. if you rotated the screen causing the Activity to be destroyed, the same Handler object would be used by the newly created Activity instance as well). In the sample code above, the Handler is declared as non-static which means that a new Handler will be created for each new Activity that is instantiated.
Yes, the Runnable is static so a single instance will be allocated and will be shared across all Activity instances (a new one will not be created for each new Activity that is created). I explain why it is important for the Runnable to be static in the second to last paragraph of the post.
java static and non-static class and variable
https://drive.google.com/file/d/0B-5-QeQdT3FxQUdnUWNFTUMxdXc/view
本文探讨了Java中静态变量与非静态内部类的区别及其可能导致的内存泄漏问题。特别是当静态变量持有Drawable引用时,可能会导致Activity无法被垃圾回收。此外,文章还解释了为什么将Handler声明为静态与非静态在内存管理上的区别。
758

被折叠的 条评论
为什么被折叠?



