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 8831511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:19:32+00:00 2026-06-14T08:19:32+00:00

So from reading/research about memory leaks it suggests to make all inner classes static

  • 0

So from reading/research about memory leaks it suggests to make all inner classes static to avoid memory leaks. However, by looking at the SDK samples (specifically TicTacToeLib) they implement their callbacks without the use of static inner classes. Will this cause a memory leak? If not, why?

private Handler mHandler = new Handler(new MyHandlerCallback());


private class MyHandlerCallback implements Callback {
        public boolean handleMessage(Message msg) {
            if (msg.what == MSG_COMPUTER_TURN) {

                // Pick a non-used cell at random. That's about all the AI you need for this game.
                State[] data = mGameView.getData();
                int used = 0;
                while (used != 0x1F) {
                    int index = mRnd.nextInt(9);
                    if (((used >> index) & 1) == 0) {
                        used |= 1 << index;
                        if (data[index] == State.EMPTY) {
                            mGameView.setCell(index, mGameView.getCurrentPlayer());
                            break;
                        }
                    }
                }

                finishTurn();
                return true;
            }
            return false;
        }
    }
  • 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-14T08:19:33+00:00Added an answer on June 14, 2026 at 8:19 am

    Yes, this sample will cause a leak in case it keeps a Message in the queue. But it’s not a very severe leak since it is usually limited to a rather short amount of time.

    But there is a rather simple way to prevent the leak:

    Put the following two classes into your project

    /** Callback that decouples the wrapped Callback via WeakReference */
    public class SafeCallback implements Handler.Callback {
        private final WeakReference<Handler.Callback> mCallback;
        public SafeCallback(Handler.Callback callback) {
            mCallback = new WeakReference<Handler.Callback>(callback);
        }
        @Override
        public boolean handleMessage(Message msg) {
            Handler.Callback callback = mCallback.get();
            if (callback != null)
                return callback.handleMessage(msg);
            // else warn, return true, ..?
            return false;
        }
    }
    

    /** replacement for anonymous inner Handler implementations */
    public abstract class SafeHandler implements Handler.Callback {
        @Override
        public abstract boolean handleMessage(Message msg);
    
        public final Handler get() {
            return new Handler(new SafeCallback(this));
        }
        public final Handler get(Looper looper) {
            return new Handler(looper, new SafeCallback(this));
        }
    }
    

    And now you can use Handler / Callback almost as you used to do but it’s no longer leaking.

    So either like

    public class TestActivity extends Activity {
        private Handler mHandler;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mHandler = new SafeHandler() { // << instead of new Handler() {
                @Override
                public boolean handleMessage(Message msg) {
                    // handle message
                    return false;
                }
            }.get(); // << Notice this added .get()
        }
    }
    

    or like

    public class TestActivity2 extends Activity implements Handler.Callback {
    
        private Handler mHandler;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mHandler = new Handler(new SafeCallback(this)); // << wrapped in SafeCallback
        }
    
        @Override
        public boolean handleMessage(Message msg) {
            // handle message
            return false;
        }
    }
    

    The leak problem with Handler is that each Message / Runnable (which is actually wrapped in a Message) knows it’s target, i.e. has a hard reference to the Handler or Callback. And if that target is a non-static inner class, it will have an implicit hard reference to the outer class which is typically an Activity.

    That means that as long as there are Messages enqueued for your Handler, your whole Activity can’t be garbage collected.

    To solve this issue that chain of hard references from Message to Activity has to be broken. The SafeCallback class does exactly that by keeping just a WeakReference towards your Activity.

    That means, the Message has now a hard reference to SafeCallback but the part bind there can now be garbage collected. In case that happens Handler.Callback callback = mCallback.get(); will turn out null and the Message is simply discarded. There is no more useful target anyways. It is still leaking the SafeCallback itself but that’s a pretty much empty class so it won’t lead to problems.

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

Sidebar

Related Questions

I’m looking at creating a P2P system. During initial research, I’m reading from Peer-to-Peer
I'm looking for some advice from the community about constructing an online store based
I was thinking about capturing some metrics from a JVM like max/min/used memory, cpu
From reading the NERDTree documentation on github, I've learned that I can do such
From reading the Apple Docs on Core Data, I've learned that you should not
From reading the RFC it appears that CID can/must only contain characters from the
It's clear from reading through threads that I can call a PHP function using
I know from reading the assoc. Google group that there is not currently an
What I have been able to grasp from reading the source and documentation from
lets say i created a virtualenv called venv (virtualenv venv) From reading tutorials, i

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.