CLR:常量字符串值在内存中的生命周期是多少?

发布于 2024-09-10 22:42:51 字数 640 浏览 3 评论 0原文

假设我们有一个包含 10000 个常量字符串成员的类。

class Schema
{
  //Average string length is 20
  public const string ID1 = "some.constant.value";
  public const string ID2 = "some.other.constant.value";
  //...
}

代码的其余部分并未引用所有字段。其中只有 10% 在启动时被访问 - 它们的引用被分配为各种字典(数千个字典实例)的键。我知道 const 字符串是被保留的 - 多次引用 const 字符串不会增加消耗的内存超过指向保留字符串表中的偏移量的元数据标记的大小。

我知道 const 字符串被编译到程序集中,从而影响编译后的程序集的大小。


这些常量字符串在什么具体时间/事件消耗运行时内存?

将是加载程序集时所有常量字符串所需的全部内存还是这个延迟到类被 JIT 编译?

我们可以通过改变等式中的某些内容来减少启动后的内存消耗吗? (使字段成为非 const,使字符串成为静态字段?)。

我们假设有一个 Winforms 应用程序 (.NET 2.0)。

Say we have a class with 10000 const string members.

class Schema
{
  //Average string length is 20
  public const string ID1 = "some.constant.value";
  public const string ID2 = "some.other.constant.value";
  //...
}

Not all fields are referenced in the rest of the code. Only 10% of them is accessed on startup - their reference is assigned as a key to various dictionaries (thousands of Dictionary instances). I understand that const strings are interned - referencing a const string multiple times does not increase consumed memory by more than the size of the metadata token pointing to an offset in the interned string table.

I understand that the const strings are compiled into the assembly and thus influence the size of the compiled assembly.


At what exact time/event do these const strings consume runtime memory?

Will be all the memory needed for all the const strings taken at the time the assembly is loaded or is this delayed until the class is JIT compiled?

Can we decrease memory consumption after startup by changing something in the equation? (make the fields non-const, make the strings static fields?).

Let's assume a Winforms application (.NET 2.0).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诺曦 2024-09-17 22:42:51

Const 字符串是编译时文字,并且由于 CLR 对这些字符串使用实习,只要应用程序处于活动状态,它们就会一直存在。

您还可以找到我对这个问题的回答 相关。

Const strings are compile time literals, and since the CLR uses interning for these they will stick around for as long as the application is alive.

You may also find my answer to this question relevant.

故事还在继续 2024-09-17 22:42:51

我可能是错的,但是当第一次引用程序集时,它完全加载到内存中
包含所有代码、元数据和常量值(我不确定嵌入式资源是否也被加载或延迟)。并且它将保持加载状态直到进程终止

I may be wrong but when the assembly is first referenced it is loaded entirely in memory
with all the code, metadata and constant values (i don't know for sure if embedded resources are loaded also or deferred). And it will remain loaded until the process is terminated

要走干脆点 2024-09-17 22:42:51

字符串是常量并不重要。常量本身根本不占用任何内存,占用内存的是文字字符串。

当您在代码中使用常量时,编译器会将其替换为对文字字符串的引用。

字符串文字随程序集一起加载,因此它们在应用程序的整个生命周期中一直存在。即使您将常量更改为其他内容,字符串文字仍然存在。使用变量而不是常量实际上会使用更多内存,因为它需要在某个地方存储变量的值(即对文字字符串的引用的副本)。

It doesn't matter that the strings are constants. The constants themselves doesn't take up any memory at all, it's the literal strings that take up memory.

When you use the constants in your code, the compiler will substitute it for a reference to the literal string.

The string literals are loaded with the assembly, so they stick around throughout the life time of the application. Even if you change your constants to something else, the string literals are still there. Using variables instead of constants will actually use more memory, because it needs somewhere to store the value of the variable (i.e. a copy of the reference to the literal string).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文