当前位置:网站首页>StringTable Detailed String Pool Performance Tuning String Concatenation
StringTable Detailed String Pool Performance Tuning String Concatenation
2022-08-01 20:34:00 【Jing <>】
1. Composition:

2. The relationship between the constant pool and the string pool:

The constant pool originally exists in the bytecode file. When running, the information in the constant pool will be loaded into the runtime constant pool. At this time, a, b, and ab are all symbols in the constant pool, and there is no changeinto a java string object, when it is executed to the line of code that references him, the a symbol will be turned into an object, and a space for the StringTable string pool will be prepared at the same time, and the generated object "a" will be used as the key.Find it in the pool, if not, put the newly generated object a into the string pool.(Note: each string object is only put into the string pool when this sentence is executed, not in advance)
The data structure of StringTable is hashtable, which has a fixed length and cannot be expanded.
3. String variable concatenation
eg: the following example is false

s3 is in the string pool, and s4 is the spliced ab. Under the action of toString, the spliced data is turned into a new string, which exists in the heap, which is equivalent to new String ("ab")
The following is the source code of toString

eg: The following example is true

s5 is because of the optimization of javac during compilation, it will consider "a" and "b" to be constants, and the splicing result is determined. The result has been determined as ab at compile time and will not change, so it is put into the string pool.The previous one is a variable, and its value may be modified at runtime, so it needs to be spliced with SringBuilder at runtime.

Lines 6 and 29 both find a symbol with a value of ab at position 4 in the constant pool, so when s3 is executed, it is found that there is no such object in the string pool, and the ab object is created according to the ab symbol, it will be put into the string pool, and when the execution reaches s5, the ab symbol will be found at position 4, so no new objects will be created, and the objects in the string pool will be used directly.
Only constant strings are stored in the string pool.
4.Characteristics of StringTable:
1. The strings in the constant pool are only symbols, and will become objects when they are used for the first time
2. Use the mechanism of string pool to avoid repeated creation of string objects
3. The principle of string variable splicing is StringBuilder (jdk1.8)
4. The principle of string constant concatenation is compile-time optimization
5. You can use the intern method to actively put the string objects that are not yet in the string pool into the string pool, and put the address (if there is one in 1.8, it will not be put in, if not, it will be put directly, regardless of whether it is there or not.All directly return the object value in the string pool. In 1.6, if there is one, it will not be put in, if not, a copy will be assigned and put in, and the object value in the string pool will be returned directly regardless of whether there is or not.)
Example:

If the position is changed as follows: x2 is successfully entered into the pool, but also exists in the heap at the same time
Version 1.6:

5. Location of StringTable


StringTable garbage collection:
StringTable will also be garbage collected, and garbage collection will occur when the memory is not very sufficient.
5.StringTable performance tuning
Two considerations:
①Mainly adjust the number of hashtable buckets,
The larger the number of buckets, the faster the operation will be.There will be better hash distribution and reduce hash conflicts
You can use -XX:StringTableSize=Number of buckets to adjust

②Consider whether to put the string object into the pool. After entering the pool, it will take up much less memory
边栏推荐
猜你喜欢
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it

Get started quickly with MongoDB

【多任务学习】Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18

Application of Acrel-5010 online monitoring system for key energy consumption unit energy consumption in Hunan Sanli Group

C语言实现-直接插入排序(带图详解)

Which websites are commonly used for patent searches?

虚拟机的IP地址自动变为127.0.0.1

【个人作品】无线网络图传模块

KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System

研究生新同学,牛人看英文文献的经验,值得你收藏
随机推荐
latex论文神器--服务器部署overleaf
【ES】ES2021 我学不动了,这次只学 3 个。
To promote energy conservation institute 】 【 the opinions of the agricultural water price reform
【Untitled】
[Multi-task optimization] DWA, DTP, Gradnorm (CVPR 2019, ECCV 2018, ICML 2018)
【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
模板特例化和常用用法
【Untitled】
Determine a binary tree given inorder traversal and another traversal method
Redis 做签到统计
第60章 ApplicationPart自动集成整体性和独立性插件项
【kali-信息收集】(1.3)探测网络范围:DMitry(域名查询工具)、Scapy(跟踪路由工具)
有用的网站
[Multi-task learning] Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18
大整数相加,相减,相乘,大整数与普通整数的相乘,相除
Redis 做网页UV统计
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
徒步,治好了我的精神内耗
[Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
9月备考PMP,应该从哪里备考?