失去内存异常 [英] getting out of memory exception

查看:82
本文介绍了失去内存异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim buff As Byte() = System.IO.File.ReadAllBytes(OpenFileDialog1.FileNames(index))
Dim ms As New System.IO.MemoryStream(buff)
Dim img As System.Drawing.Image
img = Image.FromStream(ms)
ms.Dispose()

推荐答案

您不可能通过任何软件技巧为系统添加内存。要么有更多的内存(可能需要64位系统,因为32位系统受4Gb的地址空间限制,其中一半或四分之一用于操作系统)或减少内存消耗。





另请参阅解决方案2,它更正了我关于内存限制的陈述。

[END EDIT]


如果是大图像,您可以随时将实际加载的图像的大小减小到与最大显示器的像素大小相匹配的某个大小,或者为了方便和好处缓冲,大约4-9个这样的尺寸。为此,您可以在较小的片段中打破大文件并根据请求加载它们。例如,如果每件作品的尺寸与屏幕尺寸完全相同,那么您一次不需要加载4件以上的这些作品(你可以想象吗?)。如果显示图像的区域总是小于此区域,问题就更容易了。



-SA
You cannot possibly add memory to your system by any software trick. Either have more memory (it may require having 64-bit system, because 32-bit systems are limited by 4Gb of address space, some half or a quarter of it used up by the OS) or reduce your memory consumption.


See also Solution 2 which corrects my statements about memory limitations.
[END EDIT]

In case of big images, you can always reduce the size of the actually loaded images to some size approximately matching the pixel size of the biggest monitor, or, for convenience and benefits of buffering, some 4-9 of such sizes. For that purpose, you can break you big file in smaller fragment and load them on request. For example, in case when each piece is exactly of the size of the screen, you never need to load more than 4 of such pieces at a time (can you picture that?). And if your area showing the image is always smaller than that, the problem is even easier.

—SA


我会在Code Project上阅读这篇技术博客。 .NET进程中的内存限制 [ ^ ]。



这取自上面的链接



I would have a read of this technical blog on Code Project. Memory Limits in a .NET Process[^].

This is taken from the above link

引用:

大对象的分配



也许你不知道它,但所有版本的.Net直到最后一个版本(1.0,2.0,3.0, 3.5和4.0)对单个对象可以拥有的最大大小有限制:2 GB。无论您是在64位还是32位进程中运行,都无法在单个对象中创建大于此值的任何内容。仅在版本4.5之后才删除该限制(仅适用于64位进程)。但是,除了极少数例外,如果你需要创建如此大的对象,你很可能会在你的应用程序中应用错误的设计模式。



在.Net世界中,GC将对象分为两类:小对象和大对象。您期望更具技术性的地方?是的,我也是......但就是这样。任何小于85000字节的对象都被认为是小的,任何大于该对象的对象都被认为是大的。加载CLR时,为应用程序分配的堆分为两部分:SOH(小对象堆)和LOH(大对象堆)。每种对象都存储在它的对应堆中。



说大型对象的压缩是非常昂贵的,这是非常值得注意的,所以它直接没有在当前版本中完成。 Net(开发人员表示,这种情况将来可能会发生变化)。类似于使用Large对象进行压缩的唯一操作是将两个相邻的死对象融合到一个可用内存块中,但当前没有移动大对象来减少碎片。



这篇精彩的文章提供了有关LOH的更多信息。

Allocation of big objects

Maybe you don’t know it, but all versions of .Net until the last one (1.0, 2.0, 3.0, 3.5 and 4.0) have a limit on the maximum size a single object can have: 2 GB. No matter if you are running in a 64bit or 32bit process, you cannot create anything bigger than that, in a single object. It’s only since version 4.5 when that limit has been removed (for 64 bit processes only). However, besides very few exceptions, you are very likely applying a wrong design pattern to your application if you need to create such a big objects.

In the .Net world, the GC classifies objects into two categories: small, and large objects. Where you expecting something more technical? Yeah, me too… But that’s it. Any object smaller than 85000 bytes is considered small, and any object larger than that is considered large. When the CLR is loaded, the Heap assigned for the application is divided into two parts: the SOH (Small Objects Heap) and the LOH (Large Objects Heap). Each kind of object is stored on it’s correspondent Heap.

It’s also remarkable to say that Large object’s compaction is very expensive, so it’s directly not done in current versions of .Net (developers said that this situation might change in the future). The only operation similar to compaction done with Large objects is that two adjacent dead objects are fused together into a single chunk of free memory, but no Large object is currently moved to reduce fragmentation.

This fantastic article has much more information about the LOH.


这篇关于失去内存异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆