Tag: out of memory

将大文件添加到IO.Compression.ZipArchiveEntry会抛出OutOfMemoryExceptionexception

我正在尝试使用以下代码将大型video文件(~500MB)添加到ArchiveEntry: using (var zipFile = ZipFile.Open(outputZipFile, ZipArchiveMode.Update)) { var zipEntry = zipFile.CreateEntry(“largeVideoFile.avi”); using (var writer = new BinaryWriter(zipEntry.Open())) { using (FileStream fs = File.Open(@”largeVideoFile.avi”, FileMode.Open)) { var buffer = new byte[16 * 1024]; using (var data = new BinaryReader(fs)) { int read; while ((read = data.Read(buffer, 0, buffer.Length)) > 0) { writer.Write(buffer, 0, read); } […]

如何在base64转换后释放内存

我正在尝试流式传输文件的内容。 该代码适用于较小的文件,但对于较大的文件,我会收到Out of Memory错误。 public void StreamEncode(FileStream inputStream, TextWriter tw) { byte[] base64Block = new byte[BLOCK_SIZE]; int bytesRead = 0; try { do { // read one block from the input stream bytesRead = inputStream.Read(base64Block, 0, base64Block.Length); // encode the base64 string string base64String = Convert.ToBase64String(base64Block, 0, bytesRead); // write the string tw.Write(base64String); } while […]

在WCF服务返回DataTable时需要帮助解决错误:OutOfMemoryException

我有一个正在尝试返回DataTable的WCF服务。 service方法使用SqlDataReader ,然后使用DataTable.Load()将该数据输入到它想要返回的DataTable 。 问题:当服务方法返回一个大表时(我稍后会定义它),我在调试输出中得到这些exception(它们不会削弱服务): SMDiagnostics.dll中出现类型为“System.OutOfMemoryException”的第一次机会exception SMDiagnostics.dll中发生了’System.InsufficientMemoryException’类型的第一次机会exception “大”的定义:我测试中返回的记录集包含286760条记录,当该表导出为文本时,大小约为800MB。 我知道这都是相对的,所以这可能都是毫无意义的。 大多数情况下,我指出这一点是因为对于我来说,抛出内存exception似乎相当小,特别是考虑到我正在测试的开发机器有8GB内存这一事实。 同样,它都是相对的,也许是无关紧要的,但我正在努力提供足够的信息。 这是我的连接代码: NetTcpBinding netBind = new NetTcpBinding(); netBind.Security.Mode = SecurityMode.Transport; netBind.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; netBind.MaxReceivedMessageSize = Int32.MaxValue; netBind.MaxBufferSize = Int32.MaxValue; netBind.MaxBufferPoolSize = 0; netBind.MaxConnections = 300; netBind.ListenBacklog = 300; netBind.ReaderQuotas = XmlDictionaryReaderQuotas.Max; netBind.PortSharingEnabled = true; netBind.OpenTimeout = new TimeSpan(0, 0, RegistryValues.DatabaseTimeout); netBind.CloseTimeout = new TimeSpan(0, 0, […]

虚拟和物理内存/ OutOfMemoryException

我正在开发一个64位的.Net Windows服务应用程序,它实际上会加载一堆数据进行处理。 在执行数据量测试时,我们能够压倒该进程并抛出OutOfMemoryException(当它失败时我没有关于进程的任何性能统计信息。)我很难相信该进程请求了一大块内存自从在64位计算机上运行以来,已超出该进程的允许地址空间。 我知道该进程正在一台机器上运行,该机器一直在80%-90%的物理内存使用率附近。 我的问题是:如果机器的可用物理内存严重不足,CLR是否会抛出OutOfMemoryException,即使进程不会超过其允许的虚拟内存量? 谢谢你的帮助!

垃圾收集运行得太晚 – 导致OutOfMemoryexception

想知道是否有人可以对此有所了解。 我有一个具有大内存占用(和内存流失)的应用程序。 没有任何内存泄漏,GC往往可以很好地释放资源。 但是,偶尔GC不会“按时”发生,导致内存不足exception。 我想知道是否有人可以对此有所了解? 我已经使用了REDGate分析器,这非常好 – 应用程序具有典型的“锯齿”模式 – OOM发生在锯齿的顶部。 遗憾的是,无法使用分析器(AFAIK)来识别内存流失源。 是否可以设置内存“软限制”,应该强制使用GC? 目前,GC只在内存处于绝对限制时执行,从而产生OOM。

为什么即使有大约700Mb的RAM空闲,我也会得到System.OutOfMemoryException?

我正在运行一些测试,以查看我的日志记录将如何执行而不是执行File.AppendAllText我将首先写入内存流然后复制到文件。 所以,只是为了看看内存操作有多快我就这样做了.. private void button1_Click(object sender, EventArgs e) { using (var memFile = new System.IO.MemoryStream()) { using (var bw = new System.IO.BinaryWriter(memFile)) { for (int i = 0; i < Int32.MaxValue; i++) { bw.Write(i.ToString() + Environment.NewLine); } bw.Flush(); } memFile.CopyTo(new System.IO.FileStream(System.IO.Path.Combine("C", "memWriteWithBinaryTest.log"), System.IO.FileMode.OpenOrCreate)); } } 当i达到25413324我得到了一个Exception of type ‘System.OutOfMemoryException’ was thrown.的Exception of type ‘System.OutOfMemoryException’ was […]

为什么’任何CPU(更喜欢32位)’允许我在.NET 4.5下分配比x86更多的内存?

根据许多SO答案和这篇被广泛引用的博客文章 ,为“任何CPU”构建的.NET 4.5应用程序选择了“首选32位”选项,将在32位和64位上作为32位进程运行系统(与.NET 4.0及更早版本不同)。 换句话说,选择’prefer 32-bit’的x86和AnyCPU是等效的(忽略它是否可以在ARM上运行)。 但是,我的测试表明,在64位系统上,“AnyCPU更喜欢32位”应用程序(我确认运行32位)可以分配比x86更多的内存。 我编写了一个.NET 4.5 C#控制台应用程序,它在循环中分配10MB字节数组(当然保留引用)直到它遇到OutOfMemoryException,并在具有大量RAM的64位系统上运行它。 当构建为x86时,它的分配大约为1.2GB。 构建为“任何CPU(更喜欢32位)”的相同代码最高可达1.5GB。 为什么不同?

使用gcAllowVeryLargeObjects的OutOfMemoryException

我正在使用BinarySerializer,它有一个非常大的(尽管不是很深)项目图。 我有8GB的ram支持12Gig的交换,并且在序列化时我得到一个OutOfMemoryException,这是预期的(图表可能接近或超过2Gb)。 然而,当我使用gcAllowVeryLargeObjects它并没有更好,我仍然得到相同的exception,我肯定在应该保留在内存中的东西(至少与交换)。 有什么我可以做的来支持序列化这个/一种方法来获得相同的function集但是可能会得到结果吗? 我的序列化代码没有什么特别之处: public static byte[] Serialize(this object o) { var ms = new MemoryStream(); var bf = new BinaryFormatter(); bf.Serialize(ms, o); ms.Position = 0; return ms.ToArray(); } 我正在序列化的对象包含自身包含数组等的项目数组,但完整的图形本身不是“那么大”(它是索引数据的结果,在源处,已经只有大约1GB的大小)。 这也不是由于GC碎片造成的(压缩大堆没有帮助)。

在图片框中绘制列车时,c#中的内存不足exception

我正在尝试创建一个显示图片picturebox在线列车的应用程序 因此,要实现这一点,我创建一个worker thread来获得在线列车位置。所以我定义了线程,你可以在这里看到: private Thread workerThread = null; private delegate void UpdateListBoxDelegate(); private UpdateListBoxDelegate UpdateListBox = null; 在Form_load我称之为: UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus); // Initialise and start worker thread workerThread = new Thread(new ThreadStart(this.GetOnlineTrain)); workerThread.Start(); 我委托处理的方法是: private void UpdateStatus() { foreach (TimeTable onlineTrain in OnlineTrainList.ToList()) { if (lstSensorLeft.Count != 0 || lstSensorRight.Count != 0) { pictureBoxonlineTrain.Image […]

大的,每像素1位的位图导致OutOfMemoryException

我想做的是从磁盘加载图像并从中创建一个BitmapSource 。 图像为14043px x 9933px,为b / w(1bpp)。 但是我遇到了OutOfMemoryException因为以下代码消耗大约800 MB RAM。 以下代码在我的特定文件的维度中生成ImageSource 。 我这样做是为了看看我是否可以在不使用磁盘上的实际文件的情况下使其工作。 public System.Windows.Media.ImageSource getImageSource(){ int width = 14043; int height = 9933; List colors = new List(); colors.Add(System.Windows.Media.Colors.Black); colors.Add(System.Windows.Media.Colors.White); BitmapPalette palette = new BitmapPalette(colors); System.Windows.Media.PixelFormat pf = System.Windows.Media.PixelFormats.Indexed1; int stride = width / pf.BitsPerPixel; byte[] pixels = new byte[height * stride]; for (int […]