Questions tagged [string-interning]

A string pool allows a runtime to save memory by preserving immutable strings in a pool, so that instances of common strings can be reused throughout the application instead of creating multiple instances of them.

In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.

163 questions
18
votes
4 answers

Related to String interning

public static void main(String[] args) { String a = new String("lo").intern(); final String d = a.intern(); String b = "lo"; final String e = "lo"; String c = "Hello"; System.out.println(b==a);//true …
Sumeet Sharma
  • 2,573
  • 1
  • 12
  • 24
18
votes
4 answers

Are strings pooled in Python?

Does Python have a pool of all strings and are they (strings) singletons there? More precise, in the following code, are one or two strings created in memory? a = str(num) b = str(num)
Nikolay Vyahhi
  • 1,432
  • 1
  • 16
  • 30
17
votes
4 answers

Does Python intern strings?

In Java, explicitly declared Strings are interned by the JVM, so that subsequent declarations of the same String results in two pointers to the same String instance, rather than two separate (but identical) Strings. For example: public String baz()…
csvan
  • 8,782
  • 12
  • 48
  • 91
16
votes
1 answer

Why does this string have a reference count of 4? (Delphi 2007)

This is a very Delphi specific question (maybe even Delphi 2007 specific). I am currently writing a simple StringPool class for interning strings. As a good little coder I also added unit tests and found something that baffled me. This is the code…
dummzeuch
  • 10,975
  • 4
  • 51
  • 158
16
votes
2 answers

How does string interning work in Java 7+?

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.…
kylemart
  • 1,156
  • 1
  • 13
  • 25
16
votes
2 answers

Does String.intern() really increase performance?

I did a little investigation to find out how the String.intern() method is implemented in java. I looked at C++ implementation of Intern pool from Open JDK 6 and there I saw a simple HashSet. For me it meant that when someone are trying to intern a…
Anton Kasianchuk
  • 1,177
  • 5
  • 13
  • 31
15
votes
4 answers

Why are the results of of str == str.intern() for these strings different?

public static void main(String[] args) { String str1 = new StringBuilder("计算机").append("软件").toString(); System.out.println(str1.intern() == str1); String str2 = new StringBuffer("ja").append("va").toString(); …
side
  • 209
  • 2
  • 9
14
votes
3 answers

On string interning and alternatives

I have a large file which, in essence contains data…
RobIII
  • 8,488
  • 2
  • 43
  • 93
14
votes
3 answers

What determines which strings are interned and when?

>>> s1 = "spam" >>> s2 = "spam" >>> s1 is s2 True >>> q = 'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf' >>> r = 'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf' >>> q is r False How many characters should have to s1 is s2 give False? Where is limit?…
krzyhub
  • 6,285
  • 11
  • 42
  • 68
13
votes
2 answers

What are the rules for cpython's string interning?

In python 3.5, is it possible to predict when we will get an interned string or when we will get a copy? After reading a few Stack Overflow answers on this issue I've found this one the most helpful but still not comprehensive. Than I looked at…
user5164080
13
votes
1 answer

Does .NET create a string intern pool for each assembly?

I have a situation where I will encounter lots of duplicate strings which will persist in memory for a long time. I want to use String.Intern but I don't want to invade any potential application resources since my project is a library. How does this…
toplel32
  • 1,162
  • 1
  • 7
  • 22
13
votes
3 answers

How can I do string interning in C or C++?

Is there something like intern() method in C or C++ like there is in Java ? If there isn't, how can I carry out string interning in C or C++?
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
12
votes
4 answers

Why intern() does not work with literal 'java'?

I have tried below code: public class TestIntern { public static void main(String[] args) { char[] c1={'a','b','h','i'}; String s1 = new String(c1); s1.intern(); String s2="abhi"; System.out.println(s1==s2);//true char[]…
11
votes
2 answers

How to determine the number of interned strings in Python 2.7.5?

In an earlier version of Python (I don't remember which), calling gc.get_referrers on an arbitrary interned string could be used to obtain a reference to the interned dict, which could then be queried for its length. But this is no longer working in…
jchl
  • 6,332
  • 4
  • 27
  • 51
11
votes
4 answers

c# string interning

I am trying to understand string interning and why is doesn't seem to work in my example. The point of the example is to show Example 1 uses less (a lot less memory) as it should only have 10 strings in memory. However, in the code below both…
CodingThunder
  • 923
  • 2
  • 9
  • 12
1
2
3
10 11