在短时间内防止 .NET 垃圾收集 [英] Prevent .NET Garbage collection for short period of time

查看:22
本文介绍了在短时间内防止 .NET 垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理大量数据的高性能应用程序.它在很短的时间内接收、分析和丢弃大量信息.这会导致我目前正在尝试优化的大量对象流失,但它也会导致次要问题.当垃圾收集启动时,它在清理东西时可能会导致一些长时间的延迟(我的意思是 10 到 100 毫秒).99% 的情况下这是可以接受的,但对于大约 1-2 分钟的短暂时间窗口,我需要绝对确保垃圾收集不会导致延迟.我事先知道这些时间段什么时候会发生,我只需要一种方法来确保在此期间不会发生垃圾收集.该应用程序是使用 .NET 4.0 Framework 用 C# 编写的,并且如果需要的话,它同时使用托管和非托管代码.

I have a high performance application that is handling a very large amount of data. It is receiving, analysing and discarding enormous amounts of information over very short periods of time. This causes a fair amount of object churn that I am currently trying to optimize, but it also causes a secondary problem. When Garbage Collection kicks in it can cause some long delays as it cleans things up (by long I mean 10s to 100s of milliseconds). 99% of the time this is acceptable, but for brief windows of time about 1-2 minutes long I need to be absolutely sure that Garbage Collection does not cause a delay. I know when these periods of time will occur beforehand and I just need a way to make sure that Garbage collection doesn't happen during this period. The application is written in C# using .NET 4.0 Framework and uses both managed and unmanaged code if that matters.

我的问题是;

  1. 是否可以为整个程序短暂暂停垃圾收集?
  2. 是否可以使用 System.GC.Collect() 在我需要免费垃圾收集的窗口之前强制垃圾收集,如果我这样做,我将免费垃圾收集多长时间?
  3. 人们对最大限度地减少对垃圾收集的整体需求有何建议?

注意 - 这个系统相当复杂,有很多不同的组件.我希望避免采用必须在程序的每个类上实现自定义 IDisposable 接口的方法.

Note - this system is fairly complex with lots of different components. I am hoping to avoid going to a approach where I have to implement a custom IDisposable interface on every class of the program.

推荐答案

.NET 4.6 添加了两个新方法:GC.TryStartNoGCRegionGC.EndNoGCRegion 就是为了这个.

.NET 4.6 added two new methods: GC.TryStartNoGCRegion and GC.EndNoGCRegion just for this.

这篇关于在短时间内防止 .NET 垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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