This article mainly studies the StringTable and SymbolTable of jvm.
StringTable and SymbolTable
Changes in JDK
When java7 moved the string constant pool to java heap, the string constant pool was limited to the heap memory of the entire application, and calling string.int () at runtime to increase the string constant would not make OOM permanent. Use -XX:StringTableSize to set StringTableSize, which defaults to 65536
In java8, PermGen was removed and the method area was moved to Metaspace in non-heap, so SymbolTable followed Metaspace to non-heap.
symbolic references in Runtime Constant Pool
- A complete class loading process must go through loading (
Loading) and connections (
Linking), initialization (
Initialization) these three steps
- In the class loading phase, the class loader is responsible for reading the binary bytes of a class according to its fully qualified name, flowing them into the JVM, and then converting them into a java.lang.Class object instance corresponding to the target type. What the connection phase needs to do is to merge the class data information of the binary byte stream loaded into the JVM into the runtime state of the JVM
Verification) preparation (
Preparation), parsing (
Resolution) three stages; In the initialization phase, all codes identified by static keywords in a class are executed uniformly. If static variables are executed, the initial values set in the preparation phase before will be overwritten with the values specified by the user. If the static code block is executed, the JVM will perform all operations defined in the static code block during the initialization phase.
- In connection (
Linking) step in the analysis (
Resolution) phase, all symbol references in the constant pool (
classes、interfaces、fields、methods referenced in the constant pool) to direct reference (
Get the pointer or offset of the class or field or method in memory so as to directly call the method.)
The word SymbolTable is commonly used in the implementation of traditional programming languages (
This data structure serves many of the purposes of the symbol table of a conventional programming language implementationIn the jvm, the corresponding symbolic references in the Runtime Constant Pool (
In addition to symbolic references, the Runtime Constant Pool also includes static constants.When the class is loaded (
Resolution in Linking) is created according to the constant pool table in the class metadata, so it is called runtime constantpool; This part belongs to metaspcae, in native memory
/ # jcmd 1 VM.stringtable 1: StringTable statistics: Number of buckets : 65536 = 524288 bytes, each 8 Number of entries : 23407 = 374512 bytes, each 16 Number of literals : 23407 = 2153344 bytes, avg 91.996 Total footprsize_t : = 3052144 bytes Average bucket size : 0.357 Variance of bucket size : 0.360 Std. dev. of bucket size: 0.600 Maximum bucket size : 5
- String table can be viewed at runtime using jcmdpidvm.stringtable.
/ # jcmd 1 VM.symboltable 1: SymbolTable statistics: Number of buckets : 32768 = 262144 bytes, each 8 Number of entries : 128885 = 2062160 bytes, each 16 Number of literals : 128885 = 7160912 bytes, avg 55.560 Total footprsize_t : = 9485216 bytes Average bucket size : 3.933 Variance of bucket size : 3.982 Std. dev. of bucket size: 1.996 Maximum bucket size : 14
- Symbol table can be viewed at runtime using jcmdpidvm.
See StringTable and SymbolTable at the same time.
SymbolTable statistics: Number of buckets : 32768 = 262144 bytes, each 8 Number of entries : 129215 = 2067440 bytes, each 16 Number of literals : 129215 = 7173248 bytes, avg 55.514 Total footprsize_t : = 9502832 bytes Average bucket size : 3.943 Variance of bucket size : 3.990 Std. dev. of bucket size: 1.998 Maximum bucket size : 14 StringTable statistics: Number of buckets : 65536 = 524288 bytes, each 8 Number of entries : 23470 = 375520 bytes, each 16 Number of literals : 23470 = 2157736 bytes, avg 91.936 Total footprsize_t : = 3057544 bytes Average bucket size : 0.358 Variance of bucket size : 0.361 Std. dev. of bucket size: 0.601 Maximum bucket size : 5
- The-xx:+printStringTable statistics parameter is added at startup, and SymbolTable statistics and stringtablestatistics will be output when the jvm process exits.
jcmd pid VM.native_memory
/ # jcmd 1 VM.native_memory scale=MB 1: Native Memory Tracking: Total: reserved=1857MB, committed=112MB - Java Heap (reserved=502MB, committed=32MB) (mmap: reserved=502MB, committed=32MB) - Class (reserved=1065MB, committed=47MB) (classes #8386) ( instance classes #7843, array classes #543) (malloc=1MB #21250) (mmap: reserved=1064MB, committed=45MB) ( Metadata: ) ( reserved=40MB, committed=40MB) ( used=39MB) ( free=1MB) ( waste=0MB =0.00%) ( Class space:) ( reserved=1024MB, committed=6MB) ( used=5MB) ( free=0MB) ( waste=0MB =0.00%) - Thread (reserved=29MB, committed=3MB) (thread #29) (stack: reserved=29MB, committed=2MB) - Code (reserved=243MB, committed=15MB) (malloc=1MB #4744) (mmap: reserved=242MB, committed=14MB) - GC (reserved=2MB, committed=0MB) (mmap: reserved=2MB, committed=0MB) - Internal (reserved=1MB, committed=1MB) (malloc=1MB #2172) - Symbol (reserved=10MB, committed=10MB) (malloc=7MB #223735) (arena=3MB #1) - Native Memory Tracking (reserved=4MB, committed=4MB) (tracking overhead=4MB)
- Native _ memory output contains StringTable (
interned String) and SymbolTable
- When java7 moved the string constant pool to java heap, the string constant pool was limited to the heap memory of the entire application, and calling string.int () at runtime to increase the string constant would not make OOM permanent. Use -XX:StringTableSize to set StringTableSize, which defaults to 65536; In java8, PermGen was removed and the method area was moved to Metaspace in non-heap, so SymbolTable followed Metaspace to non-heap.
- StringTable is located in heap (
java7+), while SymbolTable is in native memory; String table can be viewed at runtime using jcmdpidvm; StringTable；; SymbolTable can be viewed at runtime using jcmdpidvm.symboltable.
- Add-xx:+printstringtablestatistics parameter at startup, and output SymbolTable statistics and stringtablestatistics when jvm process exits; The Symbol part of the output using jcmd pid VM.native_memory contains StringTable (
interned String) and SymbolTable in nonhead.
- Java’s class loading mechanism
- Chapter 5. Loading, Linking, and Initializing
- 5.1. The Run-Time Constant Pool
- Understanding String Table Size in HotSpot
- Talk about PermGen and Metaspace of jvm
- Will Java’s interned strings be GCed?
- Garbage collection behaviour for String.intern()
- 10 Things Every Java Programmer Should Know about String
- Difference between String literal and New String object in Java
- Understand JVM Loading, JVM Linking, and JVM Initialization