在短时间内阻止 .NET 垃圾收集

0

本文介绍了在短时间内阻止 .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 垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14