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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:59:49+00:00 2026-06-05T16:59:49+00:00

I have a problem with a memory leak. I have this code in a

  • 0

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 Data\imgs\IMG22.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: 1536×2048
  • 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\ryder\IMG23.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/

  • 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-06-05T16:59:50+00:00Added an answer on June 5, 2026 at 4:59 pm

    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.

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

Sidebar

Related Questions

This is a C# problem. I have a big object in memory at a
I'm parsing an XML string and have a memory leak. I know this code
I appear to have a memory leak in this piece of code. It is
I have a memory leak from 3 UIImages the problem is I can't just
I have problem with this code, it's working on debug environment. On the instruments
I have a very strange memory leak problem, it seems that sqlite3_step is doing
I have a problem with a memory leak in a .NET CF application. Using
I have got a memory leak problem in the example below(u can download the
I have the following VBA code Private Sub CreateQuery_Click() Dim doc As Document Dim
I have successfully debugged my own memory leak problems. However, I have noticed some

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.