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
0
votes
2 answers

Simplest way to defeat string interning in Python

I have a case where I would like to defeat string interning. Let's say the string I have is "foo bar". I know of a few hacky/not obvious ways to defeat interning a string. All involve computing the expression at runtime. In [1]: my_str = "foo…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
0
votes
1 answer

Checking work of C# String.Intern method through ProcessHacker

I'm playing around with C# String.Intern method and have one questions. Suppose I have a program that reads a text file line by line and adds this lines to a list of strings. Let's assume that this file consists of thousands of lines of the same…
ilya
  • 41
  • 1
  • 1
  • 5
0
votes
1 answer

Is it possible to set capacity for C# string intern pool?

Normally, when init a collection such as dictionary, if we know how many key-value pairs ahead, we could use var dict = new Dictionary<>(capacity); Is it possible to set capacity for C# string intern pool? (I think internally it is like a…
AlexWei
  • 1,093
  • 2
  • 8
  • 32
0
votes
0 answers

What should be locked for the same string?

I have a method that accepts a string parameter, and the method will be called concurrently. I want to lock between calls that pass in the same string. For example, two threads pass in "abc" to call the method at the same time, then I hope they one…
ahdung
  • 350
  • 3
  • 13
0
votes
1 answer

String intern, String concatenation and String constant pool example in java

package com.lang; class StringConcatDemo2 { public static void main(String[] args) { String s1 = "Hello".concat("World"); // s1 to be created in heap. String s2 = s1.intern(); //Line-2 //Since "HelloWorld" doesn't exist in…
0
votes
3 answers

Get a key equal to an item from SortedDictionary?

Is there any way to retrieve a key from a SortedDictionary that is equal to a given object? To illustrate, lets say I create a dictionary that has a fairly memory-heavy, immutable key type: var dictionary = SortedDictionary(); var…
nonoitall
  • 1,232
  • 1
  • 15
  • 25
0
votes
1 answer

Determine whether a String is a compile-time constant

Given a reference to any String, is it possible to programmatically determine whether this is a reference to a compile time constant? Or if it's not, then whether it's stored in the intern pool without doing s.intern() == s? isConst("foo") …
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
0
votes
1 answer

String Interning in Java

I was viewing an online course on Udemy where the instructor provided the following examples for String Interning. Example 1: String s1 = "hel" + "lo"; // This is interned by JVM Example 2: String s1 = "lo"; String s2 = "hel" + s1 ; // This is…
0
votes
1 answer

Would writing millions of strings through a Writer be a memory and performance concern for the java string pool?

So say I have this code that streams back string back to a client with millions of rows of data. Would all the strings on each write be interned and thus give a hit on the java memory? If so, why in my case would it be or not be interned? I can't…
0
votes
0 answers

Could I get a out of memory error in this situation?

So in Java, Strings are interned into a pool meaning they are stored in memory automatically. In that case if I do something like: for (int i = 0; i <= 2000000000; i++){ System.out.print(i + "somelongstring"); } Could I run out if…
0
votes
4 answers

String comparison and interning

The following code (from an interview) produces an output of false, but I believe it should be true. public static void main(String[] args) { String a = "hello"; String b = a + "world"; String c = "helloworld"; …
vijay
  • 1
  • 2
0
votes
2 answers

Is it possible to monitor .Net's intern pool size?

I'm maintaining a legacy application that uses strings to lock values in a cache. It does so something like this: object Cache(string key, Func createObjToCache) { object result = Get(key); if (result == null) { string…
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
0
votes
0 answers

Are .NET string resources interned?

All of our web pages' strings come from resources. Sometimes resources are shared across pages, which seems efficient but creates an issue when somebody changes a resource without realizing how many places it is used. I am tempted to say to the…
John Wu
  • 50,556
  • 8
  • 44
  • 80
0
votes
1 answer

Does a call to ToString() on a boolean always allocate memory

Consider the following code: private bool flag; private void Test() { Console.WriteLine(flag.ToString()); } Suppose that Test() is called multiple times, will it allocate memory every time, or is there some mechanism in C# (compiler or…
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
0
votes
1 answer

Does intern() ever create a literal in the pool?

String s1=new String("abcd"); s2=s1.intern(); it is said that intern() will create a string literal in the pool if it is not already present. My question is when will this scenario arise that string object is present but not the literal? And the…
noobCoder
  • 292
  • 2
  • 17
1 2 3
10
11