Can I prevent garbage collector from stopping some

2019-07-20 02:41发布

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.

2条回答
Explosion°爆炸
2楼-- · 2019-07-20 03:18

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-07-20 03:34

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.

查看更多
登录 后发表回答