如果使用字符串作为参数,字符串是否仍然在内存中浮动?
If string is used as an argument, does string still float around the memory?
如果字符串被用作参数,它是否还在内存中??
例如,
OracleConnection conn = new OracleConnection("userid=ididid; userpassword=pwpwpw");
是否存在安全漏洞的潜在威胁?
我想知道 GC 如何处理仅用作参数的字符串...
对于作为字符串常量提供的字符串,您几乎无能为力:它们是 interned,因此在您的程序退出之前它们不会被垃圾回收。
请注意,在这种情况下使用 SecureString
无济于事,因为如果您将字符串文字的内容复制到 SecureString
的实例中,您的字符串文字将保留在您的图像中程序。
另一方面,当您的字符串源不是文字或常量时,您可以防止字符串的内容在内存中保留更长时间超出您的需要:
OracleConnection conn;
using (var pwd = new SecureString()) {
pwd.Append(...); // Append characters of the password to the string
... // Append more characters...
conn = new OracleConnection(pwd);
}
// At this point pwd is erased from memory
如果字符串被用作参数,它是否还在内存中??
例如,
OracleConnection conn = new OracleConnection("userid=ididid; userpassword=pwpwpw");
是否存在安全漏洞的潜在威胁?
我想知道 GC 如何处理仅用作参数的字符串...
对于作为字符串常量提供的字符串,您几乎无能为力:它们是 interned,因此在您的程序退出之前它们不会被垃圾回收。
请注意,在这种情况下使用 SecureString
无济于事,因为如果您将字符串文字的内容复制到 SecureString
的实例中,您的字符串文字将保留在您的图像中程序。
另一方面,当您的字符串源不是文字或常量时,您可以防止字符串的内容在内存中保留更长时间超出您的需要:
OracleConnection conn;
using (var pwd = new SecureString()) {
pwd.Append(...); // Append characters of the password to the string
... // Append more characters...
conn = new OracleConnection(pwd);
}
// At this point pwd is erased from memory