如何防止字符串被拦截(How to prevent string being interned)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
如何防止字符串被拦截(How to prevent string being interned)

我的理解(这可能是错误的)是,在c#中,当你创建一个字符串时,它会变成“intern pool”。 这保留了对字符串的引用,以便多个相同的字符串可以共享操作内存。

然而,我正在处理很多字符串,这很可能是独一无二的,我需要将它们从操作内存中彻底删除,一旦我完成了每个操作,并且我不确定缓存的引用将如何移除,以便垃圾收集器可以从内存中删除所有的字符串数据。 我该如何防止字符串在这个缓存中被执行,或者我怎样才能清除它或者从中删除一个字符串,这样就可以将它从操作内存中删除?

My understanding (which may be wrong) is that in c# when you create a string it gets interned into "intern pool". That keeps a reference to strings so that multiple same strings can share the operating memory.

However I am processing a lot of strings which are very likely unique, and I need to completely remove them from operating memory once I am done with each of them and I am not sure how the cached reference is going to be removed so that garbage collector can just remove all the string data from memory. How can I prevent the string from being interned in this cache, or how can I clear it / or remove a string from it so that it surely get removed from operating memory?

最满意答案

如果出于安全原因需要从内存中删除字符串,请使用SecureString 。

否则,如果在任何地方都没有引用字符串,那么GC将无论如何将其清理干净(不再需要实施),因此您无需担心实习。

当然,只有字符串文字才是第一位的(或者如上面Petr和其他人所说的那样调用String.Intern())。

If you need to remove the strings from memory for security reasons, use SecureString.

Otherwise, if there are no references to the string anywhere, the GC will clean it up anyway (it will no longer be interned) so you don't need to worry about interning.

And of course, only string literals are interned in the first place (or if you call String.Intern() as noted above by Petr and others).

更多推荐

本文发布于:2023-04-17 08:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0b1ddd8e1e9309d6096ea4e6339ba94d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何防止   prevent   string   interned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!