OutOfMemoryException - 克服这个问题的策略?
首先,我知道这个问题在这个论坛已经讨论过很多次了,比如 大数组 C# OutOfMemoryException 和 OutOfMemoryException
我遇到问题的对象是
Dictionary<long, Double> results
将 ID 存储为 long 并将计算结果存储为 Double
我将不得不重用相同的对象对象大约10~20次,每次当我重用它时,我都会调用 a
results = new Dictionary<long, Double>
我知道我可以将它写入文本文件或数据库文件以进行进一步处理,但如果可能的话我会尽量避免这种情况,因为它对于这我处理的数据量。我也尝试过 GC.Collect() 但没有运气。
有经验的前辈可以指点一下吗?
编辑:我有 >列表中有 300 万个对象,但它们是固定的(即所有迭代中的键都是相同的)
First of all, I am aware that this question has been discussed many times in this forum, such as
Large array C# OutOfMemoryException and
OutOfMemoryException
The object I am having problem with is
Dictionary<long, Double> results
which stores ID in long and calculation result in Double
I will have to reuse the same object about 10~20 times, every time when I reuse it I will call a
results = new Dictionary<long, Double>
I know that I can write it to a text file or database file for further processing but if possible I would try to avoid that as it is way too slow for the amount of data I handling. I have also tried GC.Collect() but no luck with that.
Can anyone with some previous experience give some pointer on this?
Edit: I have > 3 million objects in the list, but they are fixed (i.e. the key is the same in all iterations)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊 - 不。在调用中出现内存不足异常也没有什么意义。
我强烈建议您认真分析 - 将内存分析器放入程序中并找到真正的问题。除非您存储数亿对,否则长/双组合毫无意义,即使如此......
而且:64 位的萌总是明智的。由于 GC“开销”,每个进程 2 / 3 GB 的限制在 .net 上更加困难 - 不可能用完所有内存。 64 位有更高的限制。
但同样,你的指示是错误的。新词典可能根本不是错误,其他东西会浪费你的记忆。
Ah - no. Makes also little sense to get out of memory exceptions in your calls.
I STRONGLY suggest you get serious in analysing - put a memory profiler onto the program and find the real problem. a long/double combo makes zero sense unless you store some hundred million pairs, and even then....
And: A moe to 64 bit is always wise. The 2 / 3 gb limit per process is harder on .net due to GC "overhead" - impossible to use up all the memory. 64 bit has much higher limits.
But again, your indication is wrong. The new Dictionary likely is NOT the error at all, something else wastes your memory.
如果问题只是内存没有按预期释放;也许如果您在字典上使用“.Clear()”而不是每次都重新创建?
if the issue is simply that the memory isn't being freed as expected; perhaps if you use ".Clear()" on the dictionary rather than re-creating every time?
不要创建 20 个不同的实例,而是使用一个实例,但清除列表(这允许 GC 收集旧元素),以便您有更多内存可以使用。此外,如果您需要大量内存,迁移到 64 位环境可能是明智之举。
Rather than creating 20 different instances, use one, but clear the list (which allows the GC to collect old elements) so that you have more memory to work with. Also, moving to a 64 bit environment might be wise if you require huge amounts of memory.