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

  • Home
  • SEARCH
  • 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 521407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:14:50+00:00 2026-05-13T08:14:50+00:00

I posted a question not to long ago about how my program was essentially

  • 0

I posted a question not to long ago about how my program was essentially leaking memory: see here. I have now tracked it specifically to some code in which I copy the raw bytes of a Bitmap object into a managed array. The relevant code:

public class FastBitmap
{
    ...
    private byte[] rgbValues;
    private int height;
    private int width;
    private Bitmap image;

    public FastBitmap(Bitmap plainBitmap)
    {
        width = plainBitmap.Width;
        height = plainBitmap.Height;
        image = new Bitmap(plainBitmap);
        LockPixels(new Rectangle(0, 0, image.Width, image.Height));
    }

    private void LockPixels(Rectangle area)
    {
        if (locked)
            return;
        locked = true;
        BitmapData bitmapData = image.LockBits(area, ImageLockMode.ReadWrite,
                                PixelFormat.Format24bppRgb);
        IntPtr ptr = bitmapData.Scan0;
        int stride = bitmapData.Stride;
        int numBytes = image.Width * image.Height * 3;
        rgbValues = new byte[numBytes];
        for (int r = 0; r < image.Height; ++r)
        Marshal.Copy(new IntPtr((int)ptr + stride * r), 
                             rgbValues, image.Width * 3 * r, image.Width * 3);
    }
}

So thats all the extent of the code which is causing the memory not being recovered, and I imagine it has something to do with the Marshal.Copy() although I had assumed that since I was copying from a Bitmap (cleaned up on its own?) to a managed array, there was no issue.

So the question: Is it necessary to clean up the Bitmap object, either through an IDisposable interface, or something similar. What (if there is something) is inherently wrong with the use of Marshal.Copy() in this situation, and where can I clean up the issue?

Thank you

Just so you know I have tested the following code, to verify that it is indeed this causing the problem:

Bitmap bmp = new Bitmap(1000, 1000);
for (int n = 0; n < 100; ++n)
{
   FastBitmap fb = new FastBitmap(bmp);
}

At the start and end of this sequence the overall memory usage has increased by something like 320mb and doesn’t go away, regardless of how long you wait.

  • 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-13T08:14:50+00:00Added an answer on May 13, 2026 at 8:14 am

    You have forgotten to call image.UnlockBits( bitmapData );

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

Sidebar

Related Questions

I have posted a question few days ago about Querying on collections with the
I posted a similar question to this not too long ago in regards to
I have a question about this question . I posted a reply there but
This question is related to the question posted here: Why isn't my custom WCF
I recently posted a question about Azure... is it really an OS? I understand
This is in continuation with the question posted here: Finding the center of mass
Haven't seen many Geneva related questions yet, I have posted this question in the
I just posted a question about whether WPF is a good choice for a
I originally posted a question about why did the elements of my webservice return
[I just posted a similar question, but I'm not sure whether is actually got

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.