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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:43:26+00:00 2026-05-23T12:43:26+00:00

I just read a blogpost by Romain Guy on how to avoid memory leaks

  • 0

I just read a blogpost by Romain Guy on how to avoid memory leaks in Android.

In the article he gives this example:

private static Drawable sBackground;

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);

  TextView label = new TextView(this);
  label.setText("Leaks are bad");

  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);

  setContentView(label);
}

Romain said:

This example is one of the simplest cases of leaking the Context.

My question is, how do you modify it correctly?

Just like this?

TextView label = new TextView(Context.getApplicationContext());

I tested both ways and the results are the same. I can’t locate the difference. And I think that this is more correct than the Application context. Because this is a reference to Activity, that is to say, the TextView belongs to that Activity.

Could someone give me an explanation for this?

  • 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-05-23T12:43:27+00:00Added an answer on May 23, 2026 at 12:43 pm

    The actual problem with that code isn’t the context passed to create the drawable, but private static Drawable sBackground;
    The static Drawable is created with the Activity as the context, so in THAT case, there’s a static reference to a Drawable that references the Activity, and that’s why there’s a leak. As long as that reference exists, the Activity will be kept in memory, leaking all of its views.

    So it’s the Drawable which should be created using the application context, not the TextView. Creating the TextView with “this” is perfectly fine.

    edit : Actually, that might not make a big difference, the problem is that once the drawable is binded to a view, there’s a reference to the view, which references the activity. So you need to “unbind” the drawable when you exit the activity.

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

Sidebar

Related Questions

Just read this interesting article by Omar on his blog Linq to SQL solve
I just read the Wikipedia article on mock objects , but I'm still not
I just read this post about why new-line warnings exist, but to be honest
I just read Joel Spolsky's article, Up the tata without a tutu , where
I just read this requirement on a job listing: Aware of the pitfalls of
I just read this: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx I was under the impression that overwriting Object or
I just read about HTML 5's WebSocket interface. How can I start trying this?
I have just read Joel's blogpost concerning distributed version control systems and can't understand
I just read this blog post and it stated that: Google is now working
I just read this blog post about a recipe to lazily initialize an object

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.