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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:24:22+00:00 2026-05-18T04:24:22+00:00

I’ve got a custom view in which I need to draw two bitmaps, one

  • 0

I’ve got a custom view in which I need to draw two bitmaps, one is a background, representing the image of a map and one is a pin which will be drawn on top/left position in canvas.

The both images are drawn onDraw and remain the same during the live of the activity that contains the view. After a while I get a

OutOfMemoryError: bitmap size exceeds
VM budget

This means that I have a leak and the bitmaps don’t get garbage collected. I asked this question before, but now the situation changed a little. I made a init method where I set the bitmaps I want to use but this still isn’t a good approach, the error appears later, but still there.

Here is the code

public class MyMapView extends View {
private int xPos = 0;
private int yPos = 0;
private int space = 0;

private Bitmap resizedBitmap;
private Bitmap position;
private Bitmap mapBitmap;

public void setMapBitmap(Bitmap value) {
    this.mapBitmap =  value;
}

public MyMapView(Context context) {
 super(context);
}

public MyMapView(Context context, AttributeSet attrs) {
 super(context, attrs);
}

public MyMapView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
}

public void init(final Context context) {
 Paint paint = new Paint();
    paint.setFilterBitmap(true);

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    resizedBitmap = Bitmap.createScaledBitmap(mapBitmap, height, height, true);
    position = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.position);
    space = (width - resizedBitmap.getWidth()) / 2;
}

public void destroy()
{
 resizedBitmap.recycle();
 resizedBitmap=null;
 position.recycle();
 position=null;
 mapBitmap.recycle();
 mapBitmap=null;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {

     if (mapBitmap != null) {
         canvas.drawBitmap(resizedBitmap, space, 0, null);
     }

     if (xPos != 0 && yPos != 0) {
         canvas.translate(xPos + space - position.getWidth() / 2, yPos - position.getHeight() / 2);
         canvas.drawBitmap(position, new Matrix(), new Paint());

     }
}

 public void updatePosition(int xpos, int ypos)
 {
   xPos = xpos;
      yPos = ypos;
      invalidate();
 }
}

I call the setMapBitmap() and init() on onCreate of the activity that contains the view. In there I know what bitmap will be used as map. onPause of the activity I call the destroy() of the view. I still get errors.
I’ve read this this but I don’t know how to adapt it on my case

I AM OPEN TO ANY OTHER SOLUTION. Please note that I need to resize the map bitmap (based on the height of the layout that contains it in mai activity) and still be able to draw the position on correct place.

  • 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-18T04:24:22+00:00Added an answer on May 18, 2026 at 4:24 am

    You obviously have a memory leak. Read how to avoid memory leaks and how to find memory leaks.

    Here is a your code refactored to use WeakReference:

    public class MyMapView extends View {
        private int xPos = 0;
        private int yPos = 0;
        private int space = 0;
    
        private WeakReference<Bitmap> resizedBitmap;
        private WeakReference<Bitmap> position;
        private WeakReference<Bitmap> mapBitmap;
    
        public void setMapBitmap(Bitmap value) {
            this.mapBitmap = new WeakReference<Bitmap>(value);
        }
    
        public MyMapView(Context context) {
            super(context);
        }
    
        public MyMapView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyMapView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public void init(Bitmap mapBitmap) {
            Paint paint = new Paint();
            paint.setFilterBitmap(true);
    
            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
    
            resizedBitmap = new WeakReference<Bitmap>(Bitmap.createScaledBitmap(
                mapBitmap, width, height, true));
            position = new WeakReference(BitmapFactory.decodeResource(
                getContext().getResources(), R.drawable.position));
            space = (width - resizedBitmap.get().getWidth()) / 2;
        }
    
    //    public void destroy() {
    //        resizedBitmap.recycle();
    //        resizedBitmap = null;
    //        position.recycle();
    //        position = null;
    //        mapBitmap.recycle();
    //        mapBitmap = null;
    //    }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
                MeasureSpec.getSize(heightMeasureSpec));
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
    
            if (mapBitmap != null) {
                canvas.drawBitmap(resizedBitmap.get(), space, 0, null);
            }
    
            if (xPos != 0 && yPos != 0) {
                canvas.translate(xPos + space - position.get().getWidth() / 2, 
                    yPos - position.get().getHeight() / 2);
                canvas.drawBitmap(position.get(), new Matrix(), null);
    
            }
        }
    
        public void updatePosition(int xpos, int ypos) {
            xPos = xpos;
            yPos = ypos;
            invalidate();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.