GC行为和CLR线程劫持 [英] GC Behavior and CLR Thread Hijacking

查看:137
本文介绍了GC行为和CLR线程劫持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过C#阅读 CLR这本书中的GC,特别是关于CLR何时启动一个集合。我知道它必须在集合发生之前挂起线程,但它提到当线程指令指针到达安全点时它必须执行此操作。在它不是安全的情况下,它会尽快找到一个,并且它通过劫持线程来执行(在线程堆栈中插入一个特殊的函数指针)。这一切都很好,但我认为托管线程默认情况下是安全的吗?我最初以为它可能是指非托管线程,但CLR允许非托管线程继续执行,因为任何被使用的对象都应该被固定。



那么,什么是安全点在一个托管线程中,GC如何确定它是什么?



编辑:

<我认为我没有足够具体。根据这篇MSDN文章,即使当 Thread调用.Suspend ,线程在实现安全点之前不会被挂起。它进一步说明一个安全点是执行垃圾回收的线程执行点。



我想我的问题并不清楚。我意识到一个线程只能在一个安全点被暂停,并且它们必须被暂停以进行GC,但我似乎无法找到一个清晰的答案来说明安全点。什么决定了代码的安全性?


$ b


  1. 不在catch块中。

  2. 不在$ finally中
  3. 一个锁

  4. 不在p / invoke'd调用中(托管代码中)。不在CLR中运行非托管代码。

  5. 内存树是可移动的。

点#5有点令人困惑,但有时候内存树不会步行。例如,优化后,CLR可能会新建一个对象,而不是直接将其分配给一个变量。根据GC,这个对象将是一个准备收集的死物。编译器将指示GC何时发生这种情况,但不会运行GC。



以下是msdn上的一篇博文,其中提供了更多信息:http://blogs.msdn.com/b/abhinaba/archive/2009 /09/02/netcf-gc-and-thread-blocking.aspx



编辑:恩,先生,我错了#4。请参阅安全点部分中的此处。如果我们在ap / invoke(非托管)代码段内,则允许它运行,直到它再次返回到托管代码为止。然而,根据本MSDN文章,如果我们处于CLR代码的非托管部分,那么它不被认为是安全的,他们将等待代码返回到管理状态。 (至少我已经很近了)。

I was reading about the GC in the book CLR via C#, specifically about when the CLR wants to start a collection. I understand that it has to suspend the threads before a collection occurs, but it mentions that it has to do this when the thread instruction pointer reaches a safe point. In the cases where it's not in a safe point, it tries to get to one quickly, and it does so by hijacking the thread (inserting a special function pointer in the thread stack). That's all fine and dandy, but I thought managed threads by default were safe?

I had initially thought it might have been referring to unmanaged threads, but the CLR lets unmanaged threads continue executing because any object being used should have been pinned anyway.

So, what is a safe point in a managed thread, and how can the GC determine what that is?

EDIT:

I don't think I was being specific enough. According to this MSDN article, even when Thread.Suspend is called, the thread will not actually be suspended until a safe point is reached. It goes on to further state that a safe point is a point in a threads execution at which a garbage collection can be performed.

I think I was unclear in my question. I realize that a Thread can only be suspended at a safe point and they have to be suspended for a GC, but I can't seem to find a clear answer as to what a safe point is. What determines a point in code as being safe?

解决方案

'Safe Points' are where we are:

  1. Not in a catch block.
  2. Not inside a finally
  3. Not inside a lock
  4. Not inside p/invoke'd call (in managed code). Not running unmanaged code in the CLR.
  5. The memory tree is walkable.

Point #5 is a bit confusing, but there are times when the memory tree will not be walkable. For example, after optimization, the CLR may new an Object and not assign it directly to a variable. According to the GC, this object would be a dead object ready to be collected. The compiler will instruct the GC when this happens to not run GC yet.

Here's a blog post on msdn with a little bit more information: http://blogs.msdn.com/b/abhinaba/archive/2009/09/02/netcf-gc-and-thread-blocking.aspx

EDIT: Well, sir, I was WRONG about #4. See here in the 'Safe Point' section. If we are inside a p/invoke (unmanaged) code section then it is allowed to run until it comes back out to managed code again.

However, according to this MSDN article, if we are in an unmanaged portion of CLR code, then it is not considered safe and they will wait until the code returns to managed. (I was close, at least).

这篇关于GC行为和CLR线程劫持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆