是否可以读取实习生池中的所有字符串? [英] Is it possible to read all strings in the intern pool?

查看:60
本文介绍了是否可以读取实习生池中的所有字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在某些情况下,在C#中使用字符串时,CLR会将字符串实习作为一种优化.

It's well known that in some certain cases when using strings in C#, the CLR does string interning as an optimization.

所以我的问题是:

  • 是否可以读取实习生池中当前存在的所有字符串?
  • 有没有一种方法可以获取对每个实习生字符串的引用计数?
  • 是否可以从单独的进程空间读取实习生池?
  • 如果这些都不可行,那么不允许这些用例的原因是什么?

在某些情况下监视内存使用情况时,我认为这很有用.在处理敏感信息时,它也可能很有用(尽管我认为 SecureString 在许多情况下会更可取).

I could see this being somewhat useful when monitoring memory usage in certain cases. It may also be useful when working with sensitive information (although I would think SecureString would be more preferable in many scenarios).

据我所知,与字符串实习有关的唯一公共方法是 String.Intern(string) String.IsInterned(string)

As far as I can tell, the only public methods related to string interning are String.Intern(string) and String.IsInterned(string)

我是出于好奇而问,而不是试图解决一个真正的问题.我意识到,基于字符串内部存储池进行任何逻辑都是一个坏主意.

I'm asking out of curiosity, not trying to solve a real problem. I realize that doing any logic based off of the string intern pool would be a bad idea.

推荐答案

通过代码查找内联字符串没有用例,因此该功能未添加到语言中.

Looking up the interned strings via code has no use case so it's feature was not added in to the language.

但是,在调试程序时在内存中查找字符串是一种非常常见的用例,并且有一些工具可以做到这一点.

However looking up the strings in memory while debugging a program is a very common use case, and there are tools to do that.

您将需要使用Windows SDK随附的工具 WinDbg.exe .启动它并将其附加到您的程序后,您可以执行命令

You will need to use the tool WinDbg.exe that comes with the Windows SDK. After launching it and attaching it to your program you do the command

.loadby sos clr

,它将加载到扩展中以调试.NET应用程序.完成后,您可以执行命令

and that will load in the extensions for debugging .NET apps. Once you have done that you can do the command

!DumpHeap -strings

,您可以看到堆中的所有字符串对象.

and you can see all string objects in the heap.

至于告诉您正在查看的列表中的对象是否已被扣留,我不完全知道该如何做.希望您问一个有关WinDbg的新问题,以及如何判断一个字符串是否被插入,有人可以回答.

As for telling if the object in that list that you are looking at is interned or not, I am not entirely sure how. Hopefully if you ask a new question about WinDbg and how to tell if a string is interned or not someone may be able to answer.

这篇关于是否可以读取实习生池中的所有字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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