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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:03:56+00:00 2026-06-14T19:03:56+00:00

I think I have a memory leak in my Android live wallpaper. Whenever I

  • 0

I think I have a memory leak in my Android live wallpaper. Whenever I rotate the screen, the amount of memory garbage collected increases by 50kb and doesn’t go back down. I think it may be caused by a scheduled future, so I’m going to present a scenario to see if that’s the case.

Let’s say you have a class (let’s call it Foo) that has the following members.

private ScheduledFuture<?> future;
private final ScheduledExecutorService scheduler = Executors
        .newSingleThreadScheduledExecutor();

private final Runnable runnable = new Runnable() {
    public void run() {
        // Do stuff
    }
};

And now you set a scheduled future

future = scheduler.scheduleAtFixedRate(runnable, delay, speed,
                TimeUnit.MILLISECONDS);

The future holds a reference to the runnable, and the runnable holds a reference to the parent Foo object. I’m not sure if this is the case, but could this fact mean that if nothing in the program holds a reference to Foo, the garbage collector still cannot collect it because there is a scheduled future? I’m not too good at multithreading, so I don’t know if the code I’ve shown means the scheduled task will live longer than the object, meaning it won’t end up being garbage collected.

If this scenario will not result in preventing Foo from being garbage collection, I just need to be told that with a simple explanation. If it does prevent Foo from being garbage collected, then how do I fix it? Do have to do future.cancel(true); future = null;? Is the future = null part unnecessary?

  • 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-14T19:03:58+00:00Added an answer on June 14, 2026 at 7:03 pm
    • Either your run method relies on the enclosing Foo class and therefore can’t live independently. In that case I don’t see how you could have your Foo gc’ed and keep your runnable “alive” to be run by the executor
    • or your run method is static in the sense that it does not depend on the state of your Foo class, in which case you could make it static and it will prevent the problem you are experiencing.

    You don’t seem to handle interruption in your Runnable. That means that even if you call future.cancel(true) your Runnable will continue to run, which as you determined could be a cause for your leak.

    There are several ways to make a Runnable “interrupt-friendly”. Either you call a method that throws InterruptedException (like Thread.sleep() or a blocking IO method) and it will throw an InterruptedException when the future is cancelled. You can catch that exception and exit the run method promptly after having cleaned up what needs to be cleaned up and restoring the interrupted state:

    public void run() {
        while(true) {
            try {
                someOperationThatCanBeInterrupted();
            } catch (InterruptedException e) {
                cleanup(); //close files, network connections etc.
                Thread.currentThread().interrupt(); //restore interrupted status
            }
        }
    }    
    

    If you don’t call any such methods, the standard idiom is:

    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            doYourStuff();
        }
        cleanup();
    }
    

    In that case, you should try to make sure that the condition in the while is checked regularly.

    With those changes, when you call future.cancel(true), an interrupt signal will be sent to the thread executing your Runnable which will exit what it is doing, making your Runnable and your Foo instance eligible for GC.

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

Sidebar

Related Questions

I think I may have a memory leak in my LAMP application (memory gets
I've been having this memory leak issue for days and I think I have
I have some PL/SQL code that I think might have a memory leak. Everytime
I think that I have a memory leak in my app witch I can't
Alright, so I think my program might have a memory leak. It's an SDL
I have memory leak in the web application (servlet) that I am working on.
I have ask similar question before : Java String Memory Leak But I was
I have a question here: Confusing double free error message/memory leak in iPhone app
I think I tracked down a memory leak and want to confirm what I
I have a strange (looks to me) and simple memory leak situation using ARC

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.