Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8992561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:59:01+00:00 2026-06-15T22:59:01+00:00

Memory leak in below example. 1.SpeedHelper.java: public class SpeedHelper { interface Listener { void

  • 0

Memory leak in below example.

1.SpeedHelper.java:

public class SpeedHelper {

    interface Listener {
        void OnSelected(String entry);
    }
    static Listener sListener;

    static void setListener(Listener listener) {
        sListener = listener;
    }
    static Listener getListener() {
        return sListener;
    }
    static void clearListener() {
        sListener = null;
    }
}

2.CallSpeed.java

public class CallSpeed {

    protected void speed() {
        SpeedHelper.Listener litener = SpeedHelper.getListener();
        if (litener != null) {
            litener.OnSelected("mEntry");
        }
    }
}

3.MainActivity.java

public class MainActivity extends Activity {

    private CallSpeed callspeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SpeedHelper.setListener(mCallSpeedListener);
        callspeed = new CallSpeed();
        callspeed.speed();
    }
    private SpeedHelper.Listener mCallSpeedListener = new SpeedHelper.Listener() {

        @Override
        public void OnSelected(String entry) {
            Toast.makeText(getApplicationContext(), entry, Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        SpeedHelper.clearListener();
    }
}

How to avoid Memory leak for “sListener”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:59:02+00:00Added an answer on June 15, 2026 at 10:59 pm

    Your problem lies in the following non-static enclosed anonymous class in association with the static field sListener:

    private SpeedHelper.Listener mCallSpeedListener = new SpeedHelper.Listener() {
    
            @Override
            public void OnSelected(String entry) {
                Toast.makeText(getApplicationContext(), entry, Toast.LENGTH_SHORT).show();
            }
        };
    

    Because this is a non-static enclosed class, it contains a reference to its outer class, MainActivity. This hidden reference blocks the release of the MainActivity class by the GC because the object mCallSpeedListener – which contains the hidden reference – get stored in the static field sListener with the instruction SpeedHelper.setListener(mCallSpeedListener);. Therefore, the MainActivity object is never collected, its onDestroy function is never called and everything remains in memory; out of reach of the GC. Using static fields is a door wide open to memory leaks and the presence of hidden references in enclosed classes (anonymous or not) amplifies this problem.

    In your case, you will have to either remove the static field sListener or change the anonymous class to an ordinary class; one without the hidden reference to its outer MainActivity class or make a direct call to clearListener in another callback of the activity when it is terminated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a memory leak at NSDate . Please review the code below. -(void)myMethods:(NSDate
I am getting a memory leak (see >> below) in the following CLASS helper
I have got a memory leak problem in the example below(u can download the
I am facing a memory leak in placement new of standard library string. Below
Im getting the same memory leak as I mentioned the link below. NSXMLParser Leaking
I would like to know memory leak in the below mentioned code. Does JavaScript
List New Class takes up memory? Do I need C=null; in the code below?
I am hitting the below memory leak warning after analyzing my code. However, the
i had a problem with memory leak in this below code , potential leak
I am seeing a major memory leak within Firefox and IE on my below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.