哪个线程正在使用给定的对象?
- 我运行
dumpheap -type MyObjectType
命令来获取MyObjectType
的方法表。它显示堆上的三个对象。 - 我运行 dumpheap -mt
来获取地址。 - 我运行
!gcroot
命令来查找对这些对象的引用,但什么也找不到
!do
并且它提供了有关该对象的字段等的详细信息。
如何找到哪个线程正在引用或使用对象?有什么命令可以找出来吗?
- I run
dumpheap -type MyObjectType
command to get the method table forMyObjectType
. It shows three object on heap. - I run
dumpheap -mt <method table address>
to get address. - I run
!gcroot <address>
command to find the references to these objects, find nothing !do <address>
and it provides details about fields for this object etc.
How can I find which thread is referencing or using object? Any command to find that out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有简单的方法可以做到这一点,但您可以为每个线程运行
!dso
(即~*!dso
)。这将告诉您每个线程引用哪些对象。为了帮助导航输出,我通常将其记录到文件中并使用 grep 或合适的文本编辑器来搜索结果。There's no easy way to do this, but you could run
!dso
for each thread (i.e.~*!dso
). That will tell you which objects each of the threads reference. To help navigate the output I usually log it to a file and use grep or a decent text editor to search the results.您可以尝试使用 Microsoft 的 CLR Profiler 来找出哪个线程分配并访问您的对象。我不确定
!gcroot
是否显示等待终结器的对象,但 CLR Profiler 肯定会显示。您的对象也可能根本没有从任何 GC 根引用,它只是等待收集发生。这可能需要相当长的时间,特别是如果它分配在 LOB 堆中。尝试强制执行几次 GC(并查看 gen0-2 回收是否确实发生)并查看您的对象是否存活。You might try to use Microsoft's CLR Profiler to find out which thread allocated and accessed your object. I'm not sure if
!gcroot
shows objects waiting for finalizer, but CLR Profiler definitely does. Your object also might be not referenced from any GC root at all and it's just waiting for collection to occur. It can take quite some time especially if it is allocated in LOB heap. Try forcing a few GCs (and see if gen0-2 collections actually took place) and see if your object survives.