Can I prevent garbage collector from stopping some of my threads?

隐身守侯 提交于 2019-12-11 03:03:09

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/282327/can-i-prevent-garbage-collector-from-stopping-some-of-my-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!