Answered How does .Net bitmap memory allocation work?

  • Monday, April 16, 2012 3:01 PM
     
     

    Why does WPF and most of .Nets imaging libraries seem to use twice as much memory as the uncompressed size of the image?

    5000x7000x32bit (should be about 140Mb...right?)

    In my test app i have observed the following memory usage:

    • new Bitmap(filepath) - 290Mb (this is too much)
    • new Bitmap(width,height,pixelFormat) - 155Mb(this is about right)

    Is there anyway i can fix this? If this were a small image i wouldnt be so bothered, but this can have serious stability implications for a 32bit app(crashing and the like)

    Thanks.

All Replies

  • Wednesday, April 18, 2012 5:10 AM
    Moderator
     
     

    Hi rupertm88,

    Please check out below link about memory leak in WPF, in which you can find about three kind of situation when using bitmap cause a memory leak in WPF for your reference:http://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx

    Hope it helps.

    If you have any additional questions, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, April 18, 2012 7:23 AM
     
     
    could it be the extra bit/byes is using for the pixel alpha (tranparentcy) ?
  • Friday, April 20, 2012 8:38 AM
     
     

    I have looked at that page and i dont think any of them apply to my situation. I think its a bit of a stretch to say the other 140mb of usage (in my test case) is down to transparency

    I have also seen that a hosted winforms control (from a commercial imaging library) show high memory usage in WPF even when this was not the case in a winforms app.

    Im quite confused about whats going on that requires .Net to use double the amount of memory for a picture and so far i havent heard any satisfactory answers to explain it.

    The obvious (but time consuming solution) is to work on the image in tiles.

    Any tips?

  • Friday, April 20, 2012 11:17 AM
    Moderator
     
     Answered

    From your original post:

    "new Bitmap(filePath);"

    Bitmap is not a WPF class, it's System.Drawing, aka GDI+. I did a quick test with a 5000x7000 png file and I got about 145 megabytes for memory usage, not 290.

    Are you trying to display this Bitmap with WPF somehow? In that case it's possible that WPF will make a copy of the data to one of its own bitmap classes and that results in double memory usage. 

  • Friday, May 04, 2012 8:02 AM
    Moderator
     
     
    We are temporarily marking this as "Answer", if you have any concerns or new findings; please feel free to let me know.
    Best regards.

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • Friday, May 11, 2012 8:22 AM
     
     
    Perhaps i should have been clearer. This problem occurs with .Net 3.5 Sp1 using BitmapImage.  I can provide a sample to repro my issue if this is useful.
    • Edited by rupertm88 Friday, May 11, 2012 8:25 AM
    •  
  • Friday, May 11, 2012 8:35 AM
    Moderator
     
     
    Well, feel free to provide a sample. Your original post doesn't look like you are using BitmapImage.