我可以阻止垃圾收集器阻止我的一些线程吗?

时间:2022-11-11 23:48:50

Is it possible in a Compact Framework application to prevent the garbage collector from unconditionally stopping at least one of the threads, or to block GC collects at least in some portions of the code?

在Compact Framework应用程序中是否可以防止垃圾收集器无条件地停止至少一个线程,或阻止GC至少在代码的某些部分收集?

I think it has to deal with setting real time priorities, but I found a lot of advice against doing it.

我认为它必须处理设置实时优先级,但我发现了许多反对这样做的建议。

2 个解决方案

#1


3  

The GC needs to freeze all threads in order to inspect all objects. How could it do its job, if some thread is running and is modifying/creating an object?

GC需要冻结所有线程以检查所有对象。如果某个线程正在运行并且正在修改/创建对象,它怎么能完成它的工作呢?

Better don't do it.

最好不要这样做。

What you can do thogh, is to invoke GC.Collect() and GC.WaitForPendingFinalizers() before you enter in a state where you do not want to be interrupted. This will give you some time.

你可以做的就是在进入一个你不想被打断的状态之前调用GC.Collect()和GC.WaitForPendingFinalizers()。这会给你一些时间。

#2


0  

Unmanaged code is not allowed to access unpinned managed objects, but it will run without blocking during garbage collection. If you have certain routines that must keep running during garbage-collection, and they don't require access to unpinned managed objects, you could write those routines in unmanaged code and the GC wouldn't affect them.

不允许非托管代码访问未固定的托管对象,但在垃圾回收期间它将无阻塞地运行。如果您在垃圾收集期间必须继续运行某些例程,并且它们不需要访问未固定的托管对象,则可以在非托管代码中编写这些例程,GC不会影响它们。

#1


3  

The GC needs to freeze all threads in order to inspect all objects. How could it do its job, if some thread is running and is modifying/creating an object?

GC需要冻结所有线程以检查所有对象。如果某个线程正在运行并且正在修改/创建对象,它怎么能完成它的工作呢?

Better don't do it.

最好不要这样做。

What you can do thogh, is to invoke GC.Collect() and GC.WaitForPendingFinalizers() before you enter in a state where you do not want to be interrupted. This will give you some time.

你可以做的就是在进入一个你不想被打断的状态之前调用GC.Collect()和GC.WaitForPendingFinalizers()。这会给你一些时间。

#2


0  

Unmanaged code is not allowed to access unpinned managed objects, but it will run without blocking during garbage collection. If you have certain routines that must keep running during garbage-collection, and they don't require access to unpinned managed objects, you could write those routines in unmanaged code and the GC wouldn't affect them.

不允许非托管代码访问未固定的托管对象,但在垃圾回收期间它将无阻塞地运行。如果您在垃圾收集期间必须继续运行某些例程,并且它们不需要访问未固定的托管对象,则可以在非托管代码中编写这些例程,GC不会影响它们。