Home java What is interning and how to use it

What is interning and how to use it




What is Interning? What is it applied for? When should it be applied and what are pitfalls possible?

Answer 1, Authority 100%

Interning is a method of storing only one copy of many identical objects. Used in C # and Java to rows, as well as (in java) to small numbers.

Consider on the example of strings. When you say string.intern (s) in C # or s.Intern () in Java for the s line, you get a string with the same Content, but the returned string is guaranteed one and the same (i.e., the same object ) if you request an internet string with the same contents. Also, string constants are automatically online.

However, the lines obtained by another way, for example, via StringBuilder or concatenation, will not be interned, at least in the current version of languages. (However, the optimizer can pass the concatenation if it is able to calculate the arguments during compilation, so it is not worth counting on it.)


// C #
Object.ReferenceEQUALS ("123", "123") // True
Object.Ring.Intern ("12" + "3"), "123") // True
char [] chars = new [] {'1', '2', '3'};
Object.ReferenceEQUALS (New String (CHARS), New String (Chars)) // False
Object.ReferenceEQUALS (New String (Chars), "123") // False
Object.Ring.Intern (New String (Chars)), "123") // True
// java.
"123" == "123" // True
("12" + "3"). INTERN () == "123" // True
New String ("123") == New String ("123") // False
New String ("123") == "123" // False
NEW STRING ("123"). Intern () == "123" // True

This means that the internet objects can compare via referenceEquals (C #) / == (Java).

When the Intern () / Intern () method is called, the ranktime library looks through the pool of interneed objects in search of this or equal to it. If such an object is located, it is returned if not, this object is internally and returns.

What can you use this? For example, you can reduce the program memory consumption if a large number of lines are used in it, including many duplicates. For example, you have a huge XML file consisting of almost identical records. Or huge text of the program on some programming language. Then in some cases you can reduce memory consumption by intercourse rows: for example, all instances of While will be the same object.

Attention! By itself, a string read from the file is not internet, even if it is equal to some internet to the line.

Note, however, that one day an internet line cannot be “deinterned”, and it will take the memory of the program even when you won’t need anymore. Therefore, keep in mind that the innerning of the rows can also have a negative effect on the memory consumption program!

So if you decide to apply an internet in your program, be sure to integrate the memory consumption and make sure your optimization really improves the situation! (However, this applies to almost all optimizations.)

Next, the innerning of the string makes a search in global structures, and therefore will certainly require a global blocking. Therefore, several streams that actively apply internet will “fight” for the overall resource.

Another advantage of the interneed lines is that they can be compared faster. For example, if you disassemble the software text, and all keywords are internally, you can compare them as objects (which, of course, is much more frees).

In .NET, you can control whether the automatic internet of the string constants at the assembly level will be applied (Assembly). By default, string constants, as mentioned above, are internally, but you can prohibit it by specifying the compilingRelaxations.nostringInternering .

In Java, in addition to lines, packed (boxed) numbers are also interviewed. For example, packaged Constants of Types Integer and Long within -128 to 127, Boolean and Byte stored in the pool interneed objects. Example:

integer x = 1;
Integer y = 1;
Integer Z = New Integer (1);
x == y // True
y == z // false

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions