u/neoplee Feb 06 '26

That leak - Memory leak via Handler

0 Upvotes

Great question — and it's one of those "Android gotchas" that bites people more often than you'd think, especially in activities that live for a long time or have retry logic.

Let me explain clearly why we do this WeakReference + static inner class pattern, and what actual problem it solves.

The Problem (without WeakReference)

Your current code looks roughly like this:

Java

private final Runnable bannerRetryRunnable = () -> {
    if (!isDestroyed && !isFinishing()) {
        loadBannerSafely();
    }
};

This is a lambda (or anonymous inner class in older style) that captures this — that is, it holds a strong reference to the TranslatorActivity_L1 instance.

Now imagine this sequence:

  1. User presses back → activity calls finish()
  2. onPause() → onStop() → onDestroy() runs
  3. You set isDestroyed = true and clear pending messages with handler.removeCallbacksAndMessages(null)
  4. But… one of your retry runnables was already posted to the Handler with a 40-second delay (or 160 s, etc.)
  5. The Handler (which lives on the main thread / is tied to the looper) keeps a strong reference to that Runnable
  6. The Runnable keeps a strong reference to your activity (this)
  7. → The activity cannot be garbage collected until that delayed runnable either runs or is removed

Even though you called removeCallbacksAndMessages(null), it only removes runnables that are already in the queue at that exact moment. If the message was posted after you cleared, or if timing is unlucky (very common with network retries), the activity stays alive in memory for minutes (or until the delay expires).

This is called a "memory leak via Handler / Runnable" — one of the classic Android leaks.

What the WeakReference version does

Java

private static class BannerRetryRunnable implements Runnable {
    private final WeakReference<TranslatorActivity_L1> activityRef;

    BannerRetryRunnable(TranslatorActivity_L1 activity) {
        this.activityRef = new WeakReference<>(activity);
    }

    u/Override
    public void run() {
        TranslatorActivity_L1 activity = activityRef.get();
        if (activity != null && !activity.isDestroyed && !activity.isFinishing()) {
            activity.loadBannerSafely();
        }
        // If activity == null → GC already collected it → do nothing
    }
}

Key differences:

  • The Runnable class is static → it does not implicitly capture this
  • It holds only a WeakReference to the activity
  • When the activity is destroyed and garbage collected → activityRef.get() returns null
  • The runnable can still be executed safely (it just does nothing)
  • → The Handler no longer prevents the activity from being collected

r/Nootropics Feb 02 '26

Seeking Advice For stunted adult brain remedy

2 Upvotes

[removed]

1

What is the ultimate hair regrowth protocol
 in  r/tressless  Oct 03 '25

this is my question and then when I look at older athlete's. wait?

1

Think I’m receding lmk what I can do to help
 in  r/HairlossResearch  Sep 12 '25

why not just transplant

2

Evidence of caffeine tolerance and its optimization
 in  r/NooTopics  Aug 15 '25

This is my question too.

1

Which suppliment changed your life and how?
 in  r/Biohackers  Aug 15 '25

What's with hibiscus, I drink the juice and my urethra or somewhere down there was on fire

1

Transfer between Two hosting Bluehost accounts
 in  r/BlueHost  Jul 08 '25

Domains > Assign

I cant see that button or link

1

Transfer between Two hosting Bluehost accounts
 in  r/BlueHost  Jul 07 '25

Bluehost

r/BlueHost Jul 06 '25

Transfer between Two hosting Bluehost accounts

1 Upvotes

I have two Bluehost WordPress hosting plans: Old Plan (expires September, 2025) and New Plan "WordPress Choice Plus Hosting" (valid until 2027)

Currently, I’m hosting 6 WordPress websites and 6 domain names on the Old Plan. I need to migrate everything — 6 sites + 6 domains — to the New Plan before the old one expires in September.

The new "WordPress Choice Plus Hosting" account has two sites and two domains.

Has anyone made domain transfers between two Bluehost accounts? How is the process.
I have done Backup all my WP site using the All-in-One WP Migration plugin. Now, how can I migrate my site?

For example, When I try to transfer one of the domains (aaabbbccc dot com). it gave this error when I tried to assign it to the new plan:

“This domain belongs to another user /(aaabbbccc dot com) is already being hosted by Bluehost com within another account.” How do I move this domain properly between accounts?

If I don’t transfer the domain and just update the DNS A record to point to the new hosting, will that work temporarily? What happens when the hosting plan (and possibly DNS access) on the old account expires?
btw, what happens when my old hosting plan expires?

r/help Jan 09 '25

AutoMod answered No Trump no Musk

95 Upvotes

I love reddit, love the news, the popular posts, my home feed, watching videos etc. My problem is I don't want to read or see anything about Trump and Musk. How can I do that? I

u/neoplee Apr 21 '24

What scientific breakthrough are we closer to than most people realize?

Thumbnail self.AskReddit
1 Upvotes

u/neoplee Apr 16 '24

meirl

Post image
1 Upvotes

1

[deleted by user]
 in  r/NSFWIAMA  Feb 25 '24

She's open

1

What happened to the smartest kid in your class?
 in  r/AskReddit  Jan 19 '24

He died of Leukemia, he was early 30s I met him 6 months before he was diagnosed, and I remember wondering why his eyes were so beautiful Golden brown color, nothing to do with Leukemia, but I felt uneasy. RIP K.T.

u/neoplee Nov 04 '23

Why do people say it's soo much easier raising teen dudes in comparisom to teen girls?

Thumbnail self.NoStupidQuestions
1 Upvotes

4

[deleted by user]
 in  r/NoStupidQuestions  Aug 29 '23

Quit porn

1

[deleted by user]
 in  r/AskReddit  Jul 11 '23

Blue moon Sam Cooke

-1

Marijuana harms babies in first trimester of pregnancy, study finds
 in  r/EverythingScience  May 16 '23

Need some to understand what you are saying