问题描述
我有一个内存泄漏问题。
I have a problem with a memory leak.
我在这个code一 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
当我在我的笔记本电脑上运行的。exe这code工作得很好(我是指根据与完整的.NET框架的windows7 / 32位),但是当我运行在一个设备中的应用与WindowsMobile的6.1应用程序引发此异常:
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
在
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
....
图片大小约为200KB和宽度和高度周围1500px。
图像的细节:
The image size is around 200kb and the width and height around 1500px.Details of image:
- 尺寸:最高1536x2048
- 水平分辨率:72dpi的
- 水平分辨率:72dpi的
- 位深度:24
- 分辨率单位:2
- 颜色重新presentation:sRGB的 -
任何帮助这将是真正的AP preciated。
Any help it will be really appreciated.
我尝试@asawyer的code甚至删除所有code,参考等,问题不断,我想这是一些有关的图像或紧凑的框架宽度/高度。
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.
任何其他意见?
问题的解决方案和解释
测试出头真正的问题后好了,这不是一个内存泄漏,就像@pdriegen表示,其可用内存的问题。
Solution and explanation of the problemWell after test somethings the real problem it was not a memory leak, just as @pdriegen said its a problem of memory available .
我改变我的code本(以及在移动设备测试):
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()
和与code以上(显然)我得到一个字节()图像(阵列)使用的数据集在数据库中存储。
And with the code above (apparently) I get a byte() (array) of the image to store in the database using dataSet.
在结论:加载一个位图对象的MemoryStream,坏主意。
非常感谢大家谁需要它的时间来阅读我的问题,特别是那些谁张贴自己的答案。
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.
在几个星期后,这可能是最好的(免费)的解决方案:
如在此说明实施ImageHelper: ImageHelper
After a few weeks, this probably the best (for free) solution:Implement an ImageHelper as is explained here: ImageHelper
本类/示例使用OpenNetCF绘图空间( )
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.
关于克里斯·塔克
我只是意识到,张贴关于ImageHelper和OpenNetCF的创始人是在这里计算器,这里的作者是他的个人资料:
推荐答案
我不相信这个问题是内存泄漏。相反,问题是缺乏可用内存。
I don't believe the problem is a memory leak. Instead, the problem is a lack of available memory.
尽管COM pressed图像大小为200KB,当你加载它作为一个位图将是DECOM pressed并存储在本机位图格式存储。给定一个高度和每个1500px宽度,并假设32bpp的(不指定时默认)的位图格式,你看9MB分配的内存
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。
1500 * 1500 * 4 = 9MB.
由于在移动设备操作系统的内存限制present(32MB /过程 - 通过系统DLL分配的空间),你可能会在内存紧缩方案。它是未知的对我有什么其他的内存是由正运行在这个code中的应用程序分配的,当然。
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.
尝试在同一code。与较小的图像在同一设备上。你应该看到它执行罚款。
Try the same code on the same device with a smaller image. You should see that it executes fine.
这篇关于OutOfMemoryException异常装载大图像的Bitmap对象的精简框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!