In Java, the definition and use of inner classes are generally members of inner classes and anonymous inner classes, and their objects implicitly hold references to external class objects, affecting the collection of external class objects.
The GC reclaims only objects that are not referenced or are unreachable by the root set (depending on the GC algorithm), and the inner class always holds references to objects in the outer class for the life cycle, causing the objects of the outer class to never satisfy the GC's recycling condition, which is reflected in memory as memory leaks. (e.g., memory leaks from activity in Android)
Solution for
1. Define the inner class as static
2. Use static variables to refer to an instance of an anonymous inner class
Test code
- Class Outer {
- class Inner {
- Public String publicstring = "inner.publicstring";
- }
- Other anonymousother = New Other () {
- Public String publicstring = "Anonymous other.publicstring";
- };
- Public other Getanonymousother () {
- return anonymousother;
- }
- Other other = new Other ();
- Public other Getother () {
- return to other;
- }
- }
- Class Other {
- Public String publicstring = "other.publicstring";
- }
Calling code
- Public static void Main (String args[]) {
- Printfield (new Outer (). New Inner ());
- System.out.println ("\ t");
- Printfield (new Outer (). Getanonymousother ());
- System.out.println ("\ t");
- Printfield (new Outer (). Getother ());
- }
Test results
- Class:at.miao.outer$inner
- Variable: publicstring value is inner.publicstring
- Variable: this$0 value is [email protected]
- Class:at.miao.outer$1
- Variable: publicstring value is Anonymous other.publicstring
- Variable: this$0 value is [email protected]
- Class:at.miao.Other
- Variable: publicstring value is other.publicstring
You can see that an instance of an inner class and an anonymous inner class has an external class type that has a variable named this$0 that points to an object of the outer class.
After adding the static, the code is
- Class Outer {
- static class Inner {
- Public String publicstring = "inner.publicstring";
- }
- static Other anonymousother = New Other () {
- Public String publicstring = "Anonymous other.publicstring";
- };
- Public other Getanonymousother () {
- return anonymousother;
- }
- Other other = new Other ();
- Public other Getother () {
- return to other;
- }
- }
- Class Other {
- Public String publicstring = "other.publicstring";
- }
Calling code
- public static void main (string args[]) {
- printfield (new outer.inner ());
- system.out.println ( Span class= "string" > "\ t");
- Printfield (new outer (). Getanonymousother ());
- system.out.println ( "\ t");
- printfield (
- }
Test results
- Class:at.miao.outer$inner
- Variable: publicstring value is inner.publicstring
- class:at.miao.outer$1
- Variable: publicstring value is Anonymous other.publicstring
- Class:at.miao.Other
- Variable: publicstring value is other.publicstring
You can see that instances of static inner class instances, static references to anonymous inner classes do not reference instances of external classes.
Java internal classes hold references to external classes detailed analysis and solutions