C#中的字符串常量内存池 [英] String Constant Memory pool in C#

查看:229
本文介绍了C#中的字符串常量内存池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都知道,在.Net框架中,字符串对象直接存储在堆内存中

Everybody knows that in .Net framework String objects are directly stored in heap memory

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

I am just trying to understand if there is any reserved memory in .Net framework for Strings. In java there is a reserved memory for strings called SCMP(String Constant Memory Pool) where strings are initialized and garbage collected just like other objects in heap memory.

推荐答案

我认为.Net中没有类似的内容.

I dont think there is anything like that in .Net.

相反,我已阅读 及其有趣的字符串使用方式:

Instead I have read this and its interesting how Strings are used:

CLR维护一个称为内部池的表,该表包含以下内容: 程序中的文字字符串.这样可以确保重复使用 您代码中相同的常量字符串将使用相同的字符串 参考. System.String类提供了一个Intern方法,该方法可以 确保字符串在内部缓冲池中,并返回对 它.

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#中的字符串常量内存池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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