系统内存不足异常 [英] System out of memory exception

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

问题描述

Hello Friends,



我创建了一个C#控制台应用程序,我需要每周7天,每天24小时运行,以持续执行数据库请求,并对插入的数据。它工作24小时很好,但在此之后它会给内存完全异常。



我真的需要这个解决方案。



请建议一个解决方案,任何帮助将不胜感激。



谢谢,

Jagjot

Hello Friends,

I have created a C# console application which I need to run on 24/7 basis to continuously perform database requests and do heavy calculations on the data inserted. It works fine for 24 hours, but after that it gives memory full exception.

I really need solution for this.

Please suggest a solution, any help will be appreciated.

Thanks,
Jagjot

推荐答案

确保你 Dispose 你的SqlConnection和SqlCommand对象。

我假设你使用它们。

任何非托管代码?



干杯
Make sure you Dispose of your SqlConnection and SqlCommand objects.
I assume you use them.
Any unmanaged code?

Cheers


我同意Estys的答案,但你说它对你不起作用了。所以,除此之外:



您不应该仅仅因为垃圾收集工作而假设内存泄漏。许多人通过拿着垃圾收集器来允许泄漏。您的应用程序设计容易出现此问题。对于(一个典型的)示例,如果通过将数据库中找到的某些中间值添加到某个字典或其他容器来提高计算性能,则在忘记清理容器时它将保留引用。 (您可能还需要了解弱引用。)



因此,请检查您的计算部分。

问自己:什么是在循环外访问的变量。检查;尽可能地摆脱它们。

另外,通过循环周期收集一些内存消耗统计数据。
I agree withe the Answer by Estys, but you say it did not work for you -- yet. So, in addition to that:

You should not assume that a memory leak is not possible only because a garbage collection works. Many people allow the leak by holding garbage collector. Your application design is the one prone to this problem. For (one typical) example, if you improve performance of calculations by adding some intermediate values found in database to some dictionary or other container, it will hold references if you forget to clean up the container. (You may need to learn about weak references as well.)

So check up you calculation part as well.
Ask yourself: what are the variables accessed outside the loop. Check up; get rid of them as much as possible.
Also, gather some memory consumption statistics through the loop cycles.


你或者某处有内存泄漏(怀疑,但可能),或手柄泄漏。两者都会给你Out of Memory错误。



你可以通过打开TaskManager来查看是否有手柄泄漏,转到View,Select Columns ...... ,并打开Handles。开始您的过程,让它运行几次迭代。记下你得到的数字。然后在几个小时后回来比较数字。如果手柄数量相当高,你就不会处理一个物体而不是你应该使用的物体。



如果你的过程超过5,000,我认为这是一个明确的可能性8,000把手。在Windows开始做非常奇怪的事情之前,我已经看到糟糕的失控进程消耗了超过100,000个句柄(非常糟糕!)。



它可能也不是你的过程导致问题的原因,所以看看一切!



当新手尝试在Windows窗体应用程序中进行自己的绘图而不知道他们必须Dispose时,这个错误经常发生他们创造的每个GDI对象,通常是钢笔和画笔。他们最终在每次调用Paint事件时创建一个新的Pen或Brush,从不丢弃之前的那些,最终他们得到Out Of Memory错误。
You've either got a memory leak somewhere (doubtful, but possible), or a handle leak. Both will give you the Out Of Memory error.

You can see if you've got a handle leak by opening TaskManager, go to View, Select Columns..., and turn on Handles. Start your process and let it run through a few iterations. Jot down the numbers you get. Then come back in a few hours and compare the numbers. If the handles numbers are considerably higher, you're not Disposing an object than you should be.

I'd consider this a definite possiblity if your process exceeds 5,000 to 8,000 handles. I've seen bad run-away processes consume over 100,000 handles (very bad!) before Windows starts to do very strange things.

It may also not be YOUR process that's causing the problem, so look at everything!

This error happens quite frequently when newbies try to do their own drawing in Windows Forms app without knowing that they have to Dispose every GDI object they create, commonly Pens and Brushes. They end up creating a new Pen or Brush on every call of their Paint event, never disposing the previous ones, and eventually they get an Out Of Memory error.


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

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