String-interning at compiletime for profiling

Identical literal strings are not guaranty to be identical, but you can build type from it which can compare identical (without comparing string), something like: // Sequence of char template <char…Cs> struct char_sequence { template <char C> using push_back = char_sequence<Cs…, C>; }; // Remove all chars from char_sequence from ‘\0’ template <typename, char…> struct … Read more

Intern string literals misunderstanding?

String literals get interned automatically (so, if your code contains “lalala” 1000 times, only one instance will exist). Such strings will not get GC’d and any time they are referenced the reference will be the interned one. string.Intern is there for strings that are not literals – say from user input or read from a … Read more

Why do (only) some compilers use the same address for identical string literals?

This is not undefined behavior, but unspecified behavior. For string literals, The compiler is allowed, but not required, to combine storage for equal or overlapping string literals. That means that identical string literals may or may not compare equal when compared by pointer. That means the result of A == B might be true or … Read more

Garbage collection on intern’d strings, String Pool, and perm-space

String literals are interned. As of Java 7, the HotSpot JVM puts interned Strings in the heap, not permgen. Prior to java 7, hotspot put interned Strings in permgen. However, interned Strings in permgen were garbage collected. Apparently, Class objects in permgen are also collectable, so everything in permgen is collectable, though permgen collection might … Read more

String interning in .Net Framework – What are the benefits and when to use interning

In general, interning is something that just happens, automatically, when you use literal string values. Interning provides the benefit of only having one copy of the literal in memory, no matter how often it’s used. That being said, it’s rare that there is a reason to intern your own strings that are generated at runtime, … Read more