Java internal classes hold references to external classes detailed analysis and solutions

Source: Internet
Author: User

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

  1. Class Outer {
  2. class Inner {
  3. Public String publicstring = "inner.publicstring";
  4. }
  5. Other anonymousother = New Other () {
  6. Public String publicstring = "Anonymous other.publicstring";
  7. };
  8. Public other Getanonymousother () {
  9. return anonymousother;
  10. }
  11. Other other = new Other ();
  12. Public other Getother () {
  13. return to other;
  14. }
  15. }
  16. Class Other {
  17. Public String publicstring = "other.publicstring";
  18. }

Calling code

  1. Public static void Main (String args[]) {
  2. Printfield (new Outer ().   New Inner ());
  3. System.out.println ("\ t");
  4. Printfield (new Outer (). Getanonymousother ());
  5. System.out.println ("\ t");
  6. Printfield (new Outer (). Getother ());
  7. }

Test results

    1. Class:at.miao.outer$inner
    2. Variable: publicstring value is inner.publicstring
    3. Variable: this$0 value is [email protected]
    4. Class:at.miao.outer$1
    5. Variable: publicstring value is Anonymous other.publicstring
    6. Variable: this$0 value is [email protected]
    7. Class:at.miao.Other
    8. 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

  1. Class Outer {
  2. static class Inner {
  3. Public String publicstring = "inner.publicstring";
  4. }
  5. static Other anonymousother = New Other () {
  6. Public String publicstring = "Anonymous other.publicstring";
  7. };
  8. Public other Getanonymousother () {
  9. return anonymousother;
  10. }
  11. Other other = new Other ();
  12. Public other Getother () {
  13. return to other;
  14. }
  15. }
  16. Class Other {
  17. Public String publicstring = "other.publicstring";
  18. }

Calling code

    1. public static  void main (string args[])  {  
    2.         printfield (new outer.inner ());   
    3.         system.out.println ( Span class= "string" > "\ t");   
    4.          Printfield (new outer (). Getanonymousother ());   
    5.         system.out.println ( "\ t");   
    6.         printfield (
    7.     }  

Test results

    1. Class:at.miao.outer$inner
    2. Variable: publicstring value is inner.publicstring
    3. class:at.miao.outer$1
    4. Variable: publicstring value is Anonymous other.publicstring
    5. Class:at.miao.Other
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.