我可以阻止垃圾收集器停止我的某些线程吗?
在 Compact Framework 应用程序中是否可以阻止垃圾收集器无条件停止至少一个线程,或者至少在代码的某些部分阻止 GC 收集?
我认为它必须处理设置实时优先级,但我发现很多建议反对这样做。
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?
I think it has to deal with setting real time priorities, but I found a lot of advice against doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GC 需要冻结所有线程才能检查所有对象。 如果某个线程正在运行并且正在修改/创建对象,它如何完成其工作?
最好不要这样做。
不过,您可以做的是在进入您不希望被中断的状态之前调用 GC.Collect() 和 GC.WaitForPendingFinalizers()。 这会给你一些时间。
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?
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 不会影响它们。
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.