Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

memory leaks - OutOfMemoryException loading big image to Bitmap object with the Compact Framework

I have a problem with a memory leak.

I have this code in a button_click :

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim ms As New IO.MemoryStream
    Dim bm As New Bitmap("Application DataimgsIMG22.jpg")
    bm.Save(ms, Drawing.Imaging.ImageFormat.Jpeg)
End Sub

This code works just fine when I'm running the .exe at my laptop (I mean under windows7/32bits with the full .net framework) but when I run the app in a device with WindowsMobile 6.1 the app throws this exception:

SmartDeviceProject22.exe
OutOfMemoryException

at

Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
at
System.Drawing.Image.Save(Stream stream, ImageFormat format)
at
SmartDeviceProject22.Form1.Button3_Click(Object sender, EventArgs e)
at
....

The image size is around 200kb and the width and height around 1500px. Details of image:

  • Dimension: 1536x2048
  • Horizontal Resolution: 72dpi
  • Horizontal Resolution: 72dpi
  • Bit depth: 24
  • Resolution unit: 2
  • Color representation: sRGB -

Any help it will be really appreciated.

I try the code of @asawyer even remove ALL the code,reference, etc and the problem keeps, I guess it's something about the width/height of the image or with the compact framework.

Any other advice ?

Solution and explanation of the problem Well after test somethings the real problem it was not a memory leak, just as @pdriegen said its a problem of memory available .

I change my code to this (and tested at the mobile device):

 Dim fs As IO.FileStream = IO.File.OpenRead("Application Data
yderIMG23.jpg")
 Dim arrb(fs.Length) As Byte     
 fs.Read(arrb, 0, arrb.Length)
 fs.Close()
 fs.Dispose()

And with the code above (apparently) I get a byte() (array) of the image to store in the database using dataSet.

In conclusion: load a bitmap object to memoryStream, bad idea. Many thanks to everyone who take its time to read my problem,and specially those who post their answer.

Solution (if you need to show the image in a picture box):

After a few weeks, this probably the best (for free) solution: Implement an ImageHelper as is explained here: ImageHelper

updated link to the ImageHelper https://opennetcf.com/2010/10/13/loading-parts-of-large-images-in-the-compact-framework/

This class/sample uses the Drawing NameSpace from OpenNetCF (http://www.opennetcf.com/)

It works great and it solve my memory troubles loading big bitmaps to memory, actually we load a thumbnail, so the size in memory is reduced considerably and avoid the OutOfMemory exception problem.

About Chris Tacke I just realize that the author of the post about ImageHelper and co-founder of OpenNetCF it's here at stackoverflow, here is his profile: https://stackoverflow.com/users/13154/ctacke

updated link https://opennetcf.com/2010/10/13/loading-parts-of-large-images-in-the-compact-framework/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't believe the problem is a memory leak. Instead, the problem is a lack of available memory.

Even though the compressed image size is 200kb, when you load it as a bitmap it will be decompressed and stored in memory in native Bitmap format. Given a height and width of 1500px each, and assuming a bitmap format of 32bpp (the default when not specified), you're looking at 9MB of allocated memory

1500 * 1500 * 4 = 9MB.

Given the memory constraints present in the mobile device OS (32MB/process - space allocated by system dlls), you may well be in a memory crunch scenario. It's unknown to me of course what other memory is allocated by the application you are running this code in.

Try the same code on the same device with a smaller image. You should see that it executes fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...