gpt4 book ai didi

c# - C#中的字符串常量内存池

转载 作者:IT王子 更新时间:2023-10-28 23:34:20 27 4
gpt4 key购买 nike

大家都知道.Net框架中String对象是直接存储在堆内存中的

我只是想了解 .Net 框架中是否为字符串保留了任何内存。在 Java 中,有一个为字符串保留的内存,称为 SCMP(字符串常量内存池),其中字符串被初始化和垃圾收集,就像堆内存中的其他对象一样。

最佳答案

我认为 .Net 中没有类似的东西。

相反,我已阅读 this 以及有趣的字符串是如何使用的:

The CLR maintains a table called the intern pool that contains the literal strings in a program. This ensures that repeated use of the same constant strings in your code will utilize the same string reference. The System.String class provides an Intern method that ensures a string is in the intern pool and returns the reference to it.

也可以查看 MSDN :-

We have seen numerous scenarios where the managed heap contains the same string repeated thousands of times. The result is a big working set where much of the memory is consumed by strings. In this situation, it is often better to use string interning.

关于c# - C#中的字符串常量内存池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18831746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com