C#垃圾回收? [英] C# Garbage collection?

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

问题描述

我正在编写一项服务,以使我的一些例程自动化.现在我真的只在上个月才开始学习C#,所以我还是一个新手(但到目前为止我真的很喜欢).我已经将服务设计为仅在5分钟的计时器上运行一种方法,该计时器是通过AppSettings进行的,可以运行一些检查,并在需要时组织一些事情,直到下一个间隔.

I'm writing a service right now to automate a couple of my routines. Now I've really only started learning C# within the last month or so I'm still fairly new (but really liking it so far). I've designed my service that it just runs a method on a 5 minute timer which I made via AppSettings, runs a few checks, and organizes a few things if need be and that's it until the next interval.

我很快意识到我做事的最初方式似乎存在相当严重的内存泄漏.因此,我重写了其中的大部分内容,将其嵌入使用"块中,然后在完成后进行处理.工作中确实很有帮助的开发人员向我推荐了使用中"块,但我不想在他有工作要做时对他的个人项目太在意.

I quickly realized the initial way I was doing things seems to have a pretty bad memory leak. So I've re-written much of it to embed things in "using" blocks and then disposing those when it's done. The "using" blocks were recommended to me by a developer at work who's been really helpful, but I don't like to bother him too much with my personal projects when he's got work to do.

目前,我对内存使用没有任何疑问,因为它仅使用大约25Mb的内存,但启动时仅使用大约8Mb,并且每个轮询间隔都会上升.但是,一旦达到25Mb阈值,我就可以看到是否以较低的幅度假设我认为是垃圾收集处理,然后又回升至25Mb,然后冲洗并重复.因此我的应用程序内存使用情况稳定,但似乎比实际需要的要高,所以我很好奇.

Currently I'm not really having a problem with memory usage, as it's only using about 25Mb of ram, but when it starts, it's only using about 8Mb, and with each polling interval it climbs. But once it reaches that 25Mb threshold, I can see if dip a little lower which I'm assuming is garbage collection doing things, and then it climbs back up to 25Mb, and rinse and repeat. So my applications memory usage is stable, but it just seems higher than it needs to be so I'm curious.

现在,如果我调用GC.Collect手动将内存使用量减少一半.我意识到这并不理想,因为我已经对此进行了一些研究.但是,现在我的问题真的归结为,关于内存使用和垃圾回收,.NET中是否存在某种默认阈值?我问,因为它可以解释我所看到的.

Now if I call GC.Collect manually the memory usage drops to half. I realize this isn't ideal as I've already done some research on this. But now my question really comes down to, is there a some sort of default threshold in .NET when it comes to memory usage and garbage collection? I ask because it would explain what I'm seeing.

我确实在

I did look at this page on the Process.MaxWorkingSet Property, but I'm not sure if it would make a difference at all or just potentially cause me problems.

我也尝试过针对它运行探查器,但是老实说,这对我来说仍然是新事物,而且我并不确定我在寻找什么.

I also tried running a profiler against, but to be honest, this is still new to me and I wasn't entirely clear at what I was looking for.

推荐答案

垃圾收集的条件

Conditions for a garbage collection

当以下情况之一发生时,将发生垃圾收集 正确:

Garbage collection occurs when one of the following conditions is true:

系统的物理内存不足.

>托管堆上分配的对象使用的内存 超过可接受的阈值.这个阈值是连续的 在过程运行时进行调整.

> The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.

将调用GC.Collect方法.在几乎所有情况下,您都没有 之所以调用此方法,是因为垃圾收集器连续运行. 此方法主要用于特殊情况和测试.

The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

>当垃圾收集器检测到垃圾桶中的生存率很高时 代,它增加了分配的门槛 因此,下一个系列的规模将达到 回收的内存. CLR持续平衡两个优先级:不 让应用程序的工作集变得太大,而不是让 垃圾收集需要太多时间.

> When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation, so the next collection gets a substantial size of reclaimed memory. The CLR continually balances two priorities: not letting an application's working set get too big and not letting the garbage collection take too much time.

这是msdn GC文章

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

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