String caching. Memory optimization and re-use

后端 未结 3 462
囚心锁ツ
囚心锁ツ 2021-02-05 16:54

I am currently working on a very large legacy application which handles a large amount of string data gathered from various sources (IE, names, identifiers, common codes relatin

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 17:41

    You can acheive this using the built in .Net functionality.

    When you initialise your string, make a call to string.Intern() with your string.

    For example:

    dataList.Add(string.Intern("AAAA"));
    

    Every subsequent call with the same string will use the same reference in memory. So if you have 1000 AAAAs, only 1 copy of AAAA is stored in memory.

提交回复
热议问题