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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:05:24+00:00 2026-05-17T19:05:24+00:00

I stumbled upon an OutOfMemory-Exception when working with lots of pictures (sequentially, not in

  • 0

I stumbled upon an OutOfMemory-Exception when working with lots of pictures (sequentially, not in parallel). I reproduced the behavior in some small portion of code like this:

class ImageHolder
{
    public Image Image;

    ~ImageHolder()
    {
        Image.Dispose();
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 1000; i++)
        {
            ImageHolder h = new ImageHolder() { Image = new Bitmap(1000, 1000) };
        }
    }
}

The memory usage rises and rises until I get an exception (sometimes ArgumentException, sometimes OutOfMemory Exception).

My question is NOT what I can do about this (I could implement IDisposable in ImageHolder and use a using-block, for example).

My question is rather: Why doesn’t garbage collection destroy my objects of type ImageHolder (the destructor is never called), because there’s no reference on them and I’m running out of memory!

Thanks for an explanation,

Philipp

  • 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-17T19:05:25+00:00Added an answer on May 17, 2026 at 7:05 pm

    The Bitmap class is a managed class wrapper around a big chunk of unmanaged code called GDI+. The wrapper itself uses very little memory, the actual bitmap pixels are stored (by the unmanaged code) in unmanaged memory. The garbage collector cannot touch that memory, it can only see the wrapper. This is also why Bitmap has a Dispose() method, it frees that unmanaged memory.

    The OOM you get is GDI+ telling the wrapper that it can’t allocate the unmanaged memory anymore. Yes, or ArgumentException when GDI+ randomly decides that the Width or Height you pass is too large instead of throwing OOM. GDI+ is pretty notorious for throwing uninformative exceptions.

    The finalizer isn’t called because your program bombs on the GDI+ exception first. The memory allocation that failed was not one from the garbage collected heap, it was the unmanaged code that couldn’t allocate anymore.

    The finalizer code is wrong, by the time your finalizer runs, the bitmap is probably already finalized itself. You must instead have ImageHolder implement IDisposable, like this:

        class ImageHolder : IDisposable {
            public Image Image;
    
            public void Dispose() {
                if (Image != null) {
                    Image.Dispose();
                    Image = null;
                }
            }
        }
    

    Now you’ve got a shot at preventing OOM:

            for (int i = 0; i < 1000; i++) {
                using (var h = new ImageHolder() { Image = new Bitmap(1000, 1000) }) { 
                    // do something with h
                    //...
                }
            }
    

    If you really do need to store a thousand of those large images then you’ll need a machine that can provide 1000 x 1000 x 1000 x 4 = 4 gigabytes of virtual memory. That’s possible, a 64-bit operating system can give you that.

    The generic rule of thumb to keep you out of trouble like this is that it is extremely rare to implement your own destructor. It is the job of the .NET classes to provide wrappers around unmanaged resources. Like Bitmap. Those wrapper classes have a finalizer, you don’t need (and should not) provide your own. The 99.99% case is that you need to implement IDisposable so you can call Dispose() on the .NET class instances. Even if you would be tempted to manage your own operating system resources then you still don’t. You should use one of the SafeHandle wrappers.

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

Sidebar

Related Questions

I stumbled upon a small library which has some classes in four packages and
I stumbled upon a problem from the last Facebook Hacker Cup (so it's NOT
I stumbled upon this issue with parseInt and I'm not sure why this is
I stumbled upon some code the other day that was making use of query
I stumbled upon the weirdest behavior in IE6/FF3 when setting custom height (even if
I stumbled upon a very weird behavior inside a javascript closure. If a global
I stumbled upon this weird behavior in R: > a = 5 > names(a)
I stumbled upon a weird behavior of Memcached server (version 1.4.5): I have a
I stumbled upon this oddity today while playing with some code to go down
I stumbled upon this website: http://www.liptongreenmint.ro/ I like their small and simple slideshow with

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.