数组力垃圾收集,C# [英] Force garbage collection of arrays, C#

查看:155
本文介绍了数组力垃圾收集,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里有一对夫妇3维数组分配大量内存和程序有时需要用大/小的人来取代他们,并抛出一个OutOfMemoryException。问题

I have a problem where a couple 3 dimensional arrays allocate a huge amount of memory and the program sometimes needs to replace them with bigger/smaller ones and throws an OutOfMemoryException.

例如:有5分配96MB阵列(200x200x200,每个条目12字节的数据),程序需要与210x210x210(111MB),以取代他们。它做它类似于这样的方式:

Example: there are 5 allocated 96MB arrays (200x200x200, 12 bytes of data in each entry) and the program needs to replace them with 210x210x210 (111MB). It does it in a manner similar to this:

array1 = new Vector3[210,210,210];

在哪里ARRAY1-array5是用于previously相同的字段。这应设置旧阵列作为垃圾收集的候选人,但似乎GC没有行动不够迅速和叶分配新的之前分配的旧阵列 - 这将导致OOM - 而如果他们所在的新的分配前释放的空间应够了。

Where array1-array5 are the same fields used previously. This should set the old arrays as candidates for garbage collection but seemingly the GC does not act quickly enough and leaves the old arrays allocated before allocating the new ones - which causes the OOM - whereas if they where freed before the new allocations the space should be enough.

我正在寻找一种方法可以做这样的事情:

What I'm looking for is a way to do something like this:

GC.Collect(array1) // this would set the reference to null and free the memory
array1 = new Vector3[210,210,210];

我不知道是否完整垃圾的收集将是一个好主意,因为这code可以(在某些情况下),需要进行相当频繁执行。

I'm not sure if a full garbage collecion would be a good idea since that code may (in some situations) need to be executed fairly often.

是否有这样做的正确方法?

Is there a proper way of doing this?

推荐答案

这是不是一个确切的答案,原来的问题,如何强制GC',但是,我认为它会帮助你重新审视自己的问题。

This is not an exact answer to the original question, "how to force GC', yet, I think it will help you to reexamine your issue.

看到您的评论后,


  • 把GC.Collect的();似乎帮助,altought它仍然没有彻底解决问题 - 当某种原因大约1.3GB分配(我使用System.GC.GetTotalMemory(假);找到分配的实际金额)程序仍然崩溃。

  • Putting the GC.Collect(); does seem to help, altought it still does not solve the problem completely - for some reason the program still crashes when about 1.3GB are allocated (I'm using System.GC.GetTotalMemory( false ); to find the real amount allocated).

我会怀疑你可能有内存碎片。如果对象是大(在.NET 2.0 CLR 85000字节,如果我没有记错,我不知道它是否已更改与否),对象将在一个特殊的堆上分配,大对象堆(LOH) 。 GC回收不被使用在LOH不可达的对象的记忆,然而,它不LOH因为它没有其他堆(gen0,第一代,第二代和)进行压实,,由于性能。

I will suspect you may have memory fragmentation. If the object is large (85000 bytes under .net 2.0 CLR if I remember correctly, I do not know whether it has been changed or not), the object will be allocated in a special heap, Large Object Heap (LOH). GC does reclaim the memory being used by unreachable objects in LOH, yet, it does not perform compaction, in LOH as it does to other heaps (gen0, gen1, and gen2), due to performance.

如果你经常分配和释放较大的物体,它将使LOH分散,即使你总共有比你需要更多的可用内存,您可能没有一个连续的内存空间了,因此,会得到OutOfMemory例外。

If you do frequently allocate and deallocate large objects, it will make LOH fragmented and even though you have more free memory in total than what you need, you may not have a contiguous memory space anymore, hence, will get OutOfMemory exception.

我觉得在这一刻两种解决方法。

I can think two workarounds at this moment.


  1. 将64位计算机/ OS和利用它:)(最简单的,但可能是最困难的,以及根据您的资源约束)

  2. 如果你不能做到#1,则首先尝试分配的内存一个巨大的吸盘,并使用它们(它可能需要编写一些辅助类来操纵一个较小的数组,这实际上驻留在一个更大的阵列),以避免碎片。这可能有点帮助,但是,它可能不能完全解决问题,你可能要应对的复杂性。

这篇关于数组力垃圾收集,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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