如何防止字符串被扣留 [英] How to prevent string being interned

查看:78
本文介绍了如何防止字符串被扣留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解(可能是错误的)是,在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?

推荐答案

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

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

否则,如果在任何地方都没有对该字符串的引用,则GC仍将对其进行清理(它将不再进行内部检查),因此您不必担心进行内部检查.

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.

当然,只有字符串文字会首先插入(或者,如果您调用Petr和其他人在上面提到的String.Intern()的话).

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).

这篇关于如何防止字符串被扣留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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