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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:07:56+00:00 2026-05-28T22:07:56+00:00

Following up on Storing a Bitmap resource in a static variable , it seems

  • 0

Following up on Storing a Bitmap resource in a static variable, it seems that storing a static reference to an android.graphics.Bitmap in a View may leak a reference to that first View that instantiated it. What’s the idiomatic way to solve this in Android? I do not want to call BitmapFactory.decodeResource(resource, id) each time an instance of this view is instantiated because this will be done (many times) in every single Activity. I want this small Bitmap to always remain in memory. So, what is the correct way to do the following:

public class MyView extends View {
    private static Bitmap star;
    public MyView(Context context) {
        synchronized(this) {
            if (star == null) {
                star = BitmapFactory.decodeResource(getResources(), R.drawable.star);
            }
        }
    }
    // ...
}
  • 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-28T22:07:57+00:00Added an answer on May 28, 2026 at 10:07 pm

    Create a static clean-up method in your View that you call from your Activity’s onPause(). In that call, call the bitmap’s recycle() and clear out the reference. Likewise, instead of creating the bitmap in the constructor, have an initialization call that you call in your activity’s onResume().

    If you have any concerns that there might be an overlap because your View is used across activities, you can have the init and clean-up calls maintain a reference count, so that the bitmap is only destroyed when the count hits 0. If the bitmap is small enough, you can consider onCreate()/onDestroy() as well.

    Be sure to check the bitmap reference in your view class for null before using it.

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

Sidebar

Related Questions

Following is the script for matching regex and storing value in array: sub b1
I'm storing JSON formatted data into a var adds with the following methods: var
Android:How to display images from the web in a ListView?I have the following code
I have the following .ashx page that takes some query string parameters and returns
I am using following code snippet to load url to bitmap in for loop.
I'm trying to read the bitmap header. I have defined the following struct: typedef
I have the following code : [DllImport(shell32.dll, CharSet = CharSet.Unicode, PreserveSig = false)] static
I use the following method to get a bitmap input stream: private InputStream getInputStream(String
Considering the following sample XAML file, which shows the first 1000 people of Facebook,
Please guide whats wrong with the following code that its not creating proper list

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.