string-interning

Why does .NET create new substrings instead of pointing into existing strings?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 15:15:21
问题 From a brief look using Reflector, it looks like String.Substring() allocates memory for each substring. Am I correct that this is the case? I thought that wouldn't be necessary since strings are immutable. My underlying goal was to create a IEnumerable<string> Split(this String, Char) extension method that allocates no additional memory. 回答1: One reason why most languages with immutable strings create new substrings rather than refer into existing strings is because this will interfere with

Why does .NET create new substrings instead of pointing into existing strings?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 13:59:44
From a brief look using Reflector, it looks like String.Substring() allocates memory for each substring. Am I correct that this is the case? I thought that wouldn't be necessary since strings are immutable. My underlying goal was to create a IEnumerable<string> Split(this String, Char) extension method that allocates no additional memory. One reason why most languages with immutable strings create new substrings rather than refer into existing strings is because this will interfere with garbage collecting those strings later. What happens if a string is used for its substring, but then the

Any weak interning collections (for immutable objects)

别说谁变了你拦得住时间么 提交于 2019-11-30 13:34:11
问题 In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the program's perspective, the fact that two lines have the same sequence of characters would be "coincidence", but from the programmer's perspective a large amount of duplication may be expected. If many string instances are identical, changing the references

Real Life, Practical Example of Using String.intern() in Java?

前提是你 提交于 2019-11-30 10:55:28
I've seen many primitive examples describing how String intern()'ing works, but I have yet to see a real-life use-case that would benefit from it. The only situation that I can dream up is having a web service that receives a considerable amount of requests, each being very similar in nature due to a rigid schema. By intern()'ing the request field names in this case, memory consumption can be significantly reduced. Can anyone provide an example of using intern() in a production environment with great success? Maybe an example of it in a popular open source offering? Edit: I am referring to

How does string interning work in Java 7+? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-11-30 07:58:10
问题 This question already has answers here : String pool vs Constant pool (2 answers) Closed 5 years ago . So, I realize the questions I'm about to ask relate to a topic that has been beaten to death time and time again, however, even after reading all of the answers and documentation I could find, I'm still kind of confused about string interning. Perhaps it's due to my lack of understanding for the JVM; perhaps it's due to the changes introduced in Java 7 depreciating many of the aforementioned

Any weak interning collections (for immutable objects)

纵然是瞬间 提交于 2019-11-30 07:23:59
In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the program's perspective, the fact that two lines have the same sequence of characters would be "coincidence", but from the programmer's perspective a large amount of duplication may be expected. If many string instances are identical, changing the references to those distinct instances into references to a single instance will save memory, and will also

Read the content of the string intern pool

最后都变了- 提交于 2019-11-30 04:47:59
问题 I would like to enumerate the strings that are in the string intern pool . That is to say, I want to get the list of all the instances s of string such that: string.IsInterned(s) != null Does anyone know if it's possible? 回答1: Thanks to the advice of @HansPassant, I managed to get the list of string literals in an assembly. Which is extremely close to what I originally wanted. You need to use read assembly meta-data, and enumerate user-strings. This can be done with these three methods of

Why Javascript ===/== string equality sometimes has constant time complexity and sometimes has linear time complexity?

烈酒焚心 提交于 2019-11-30 04:42:27
问题 After I found that the common/latest Javascript implementations are using String Interning for perfomance boost (Do common JavaScript implementations use string interning?), I thought === for strings would get the constant O(1) time. So I gave a wrong answer to this question: JavaScript string equality performance comparison Since according to the OP of that question it is O(N), doubling the string input doubles the time the equality needs. He didn't provide any jsPerf so more investigation

Real Life, Practical Example of Using String.intern() in Java?

爷,独闯天下 提交于 2019-11-29 16:21:59
问题 I've seen many primitive examples describing how String intern()'ing works, but I have yet to see a real-life use-case that would benefit from it. The only situation that I can dream up is having a web service that receives a considerable amount of requests, each being very similar in nature due to a rigid schema. By intern()'ing the request field names in this case, memory consumption can be significantly reduced. Can anyone provide an example of using intern() in a production environment

Can we avoid interning of strings in java?

本小妞迷上赌 提交于 2019-11-29 12:00:20
Can we completely disable interning of strings. It might not be really helpful, but just a thought. I can think atleast one point where it could be helpful i.e. during jvm tuning, controlling the size of the perm gen. For e.g. if I give out an OSGI framework and anyone can add any number of bundles of their own and each bundles string interning can completely screw up my tuning parameters. (Ofcourse I know that we should do tuning on a given fixed distro, but still...) Any thoughts!! Permgen size/usage isn't an issue with modern JVMs. Interned strings are made available to the garbage