Home > Java > javaTutorial > body text

What is the function of String.intern() method in java

WBOY
Release: 2023-05-23 17:10:06
forward
1427 people have browsed it

String.intern principle

String.intern():
This method is a Native method
The underlying method is implemented by calling C’s StringTable::intern method

When the intern() method is called through the statement str.intern()
the JVM will search in the constant pool of the current class to see if there is a String with the same value as str
If it exists, it will directly return the corresponding Strnig in the constant pool Reference
If it does not exist, an equivalent String
will be created in the constant pool and then the reference of this String in the constant pool will be returned
---------------- --------------------------------------------------
Therefore, as long as they are equivalent String objects
Using the intern() method returns the same String reference in the constant pool
Therefore, these equivalent String objects use == after passing intern() Can match

String.intern() in JDK6

The constant pool in Jdk6 is located in PermGen (permanent generation)
PermGen is a block mainly used to store the Fixed size area for loaded class information and string pool
---------------------------------- ----------------------------------------
When running the intern() method
When there is no equivalent string in the constant pool, the JVM will create an equivalent string in the constant pool
and then return a reference to the string
In addition, the JVM will automatically add a string to the constant Save a collection of previously used strings in the pool
The main problem with using the intern() method in Jdk6 is that the constant pool is saved in PermGen (permanent generation)

Notes:
First of all, PermGen is an area of ​​fixed size. Generally, the default size of PermGen is different on different platforms.
It is roughly between 32M and 96M.
Therefore, you cannot use the intern() method for uncontrolled runtime strings (such as user input information, etc.),
Otherwise, it is likely to cause PermGen memory overflow;
Secondly, the String object is stored in the Java heap Area, Java heap area and PermGen are physically isolated,
Therefore, if intern operations are performed on multiple string objects with unequal values,
it will result in many duplicate strings in the memory, which will cause performance losses.

String.intern() in JDK7

Jdk7 moved the constant pool from the PermGen area to the Java heap area
When running the intern operation
When the constant pool If the string already exists, return the string reference directly
Otherwise copy the reference of the string object to the constant pool and return
The size of the heap area is generally not limited, so move the constant pool from the PremGen area The heap area makes the use of the constant pool no longer limited to a fixed size
In addition, the objects in the constant pool located in the heap area can be garbage collected
When the string in the constant pool no longer points to it When referenced, the JVM will recycle the string
You can use the -XX:StringTableSize virtual machine parameter to set the map size of the string pool
The string pool is internally implemented as a HashMap
So when you can determine the value in the program When the number of intern strings is needed
You can set the size of the map to the required number*2 (reduce hash conflicts)
This will make String.intern() only require constant time and ## each time # A String can be stored in the string pool with a relatively small amount of memory.
Function of the String.intern() method:
Its function is to manually put the string into the constant pool

The above is the detailed content of What is the function of String.intern() method in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template