StringTable 字符串池
1. StringTable
1.1 StringTable特性
- 常量池中的字符串仅是符号,第一次用到时才变为对象
- 利用字符串池的机制【会在编译期间将常量字符串拼接】,来避免重复创建字符串对象
- 字符串变量拼接的原理是StringBuilder.append… .toString
- 字符串常量拼接的原理是编译期优化
- 可以使用intern方法,主动将串池中还没有的字符串对象放入串池
- 1.8 intern() 将这个字符串对象尝试放入串池,如果有则不会放入,没有则放入串池,会返回池中的对象
- 1.6 intern() 将这个字符串对象尝试放入串池,如果有则不会放入,没有则会把此对象复制一份放入串池,会返回池中的对象
- 1.8和1.6的区别:【1.6版本调用intern的字符串对象 和 放入池中的字符串对象是两个对象,而1.8是同一个】
1.2 问题1(串池中没有字符串常量的比较)
public static void main(String[] args) {
// 堆 new String("a"), new String("b"), new String("ab")
String s = new String("a") + new String("b");//串池 ["a","b"]
System.out.println(s == "ab"); //【1】 false s是堆中的对象 ab是串池中的常量对象
String s2 = s.intern(); //串池["a","b","ab"]
//intern() 将这个字符串对象放入串池,如果有则不会放入,没有则放入串池,会返回池中的对象
System.out.println(s2 == "ab"); //【2】 true s2是池中的对象 ab也是池中的对象
System.out.println(s == "ab"); //【3】 true s变成了串池中的对象 ab是串池中的常量
}
1.3 问题2(串池中有字符串常量的比较)
public class q1 {
public static void main(String[] args) {
String x = "ab"; // 将ab放入常量池 串池:["ab"]
// 堆 new String("a"), new String("b"), new String("ab")
String s = new String("a") + new String("b");//串池:["a","b","ab"]
String s2 = s.intern(); //串池已经有ab了 所以返回的是串池中ab的对象-->x
System.out.println(s2 == x); //【1】 true
System.out.println(s == x); //【2】 false
}
}
1.4 面试题
public static void main(String[] args) throws InterruptedException {
String s1 = "a"; //
String s2 = "b"; //
String s3 = "a" + "b"; //
String s4 = s1 + s2; // s4是 堆对象
String s5 = "ab";
String s6 = s4.intern(); //
// 问
System.out.println(s3 == s4);//【1】 False
System.out.println(s3 == s5);//【2】 True
System.out.println(s3 == s6);//【3】 True
String x2 = new String("c") + new String("d");
String x1 = "cd";
x2.intern();
System.out.println(x2 == x1);//【4】 False
}
2. StringTable的位置
- 1.6版本StringTable的位置在方法区的永久代中,这样使用的效率不高
- 1.6以上的版本StringTable的位置在堆内存中
3. StringTable 的垃圾自动回收
3.1 实例1
/*
演示垃圾回收
-Xmx10m -XX:+PrintStringTableStatistics -XX:+PrintGCDetails -verbose:gc
内存大小 打印StringTable信息 打印GC信息
*/
public static void main(String[] args) throws InterruptedException {
int sum = 0;
try {
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(sum); // 2631
}
}
默认下 键值对entries(2552)常量数literals(2552)
加一个for循环
for (int i = 0; i < 100; i++) {
String.valueOf(i).intern();
}
键值对entries(2652)常量数literals(2652)多了100个
将for循环循环60000次 键值对entries(34042)常量数literals(34042)并不是2552+60000 这是因为StringTable内存占用过多垃圾自动回收了一部分常量
4. StringTable 的优化
4.1通过设置StringTable大小优化【越大耗时越小】
/*
StringTable的优化
-Xms500m -Xmx500m -XX:+PrintStringTableStatistics -XX:StringTableSize=10000
用for循环向StringTable里加入字符串常量循环50w次
设置堆内存为500m,串池桶为10000
*/
public static void main(String[] args) throws InterruptedException {
long start = System.nanoTime();
for (int i = 0; i < 500000; i++)
String.valueOf(i).intern();
long end = System.nanoTime();
System.out.println((end - start) / 1000000 + " ms");
}
耗时:1714ms
将串池桶大小改为100000
耗时:596ms
4.2 将大量重复的字符串入池
5. 直接内存(Direct Memory)
- 属于操作系统,常见于NIO操作时,用于数据缓冲区
- 分配回收成本较高,但读写性能高
- 不受JVM内存回收管理,属于系统内存
通过直接内存ByteBuffer.allocateDirect()能比普通IO更快的读写文件的原因是:
直接内存开辟的空间能使系统内存和java堆内存共享
而IO需要在系统内存缓冲区加载资源后再传到java堆内存缓冲区
5.1 直接内存溢出
/*
演示直接内存溢出
*/
public static void main(String[] args) throws InterruptedException {
List<ByteBuffer> list = new ArrayList<>();
int i = 0;
try{
while(true){
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024 * 1024 * 10);
list.add(byteBuffer);
i++;
}
}catch (Exception e){
e.printStackTrace();
}finally {
System.out.println(i);
}
}
内存溢出:不能分配10大小的直接内存(已分配:158…,限制:158…)
OutOfMemoryError: Cannot reserve 10485760 bytes of direct buffer memory (allocated: 1583357952, limit: 1587544064)
5.2 直接内存的释放
5.2.1 分配内存
long base = unsafe.allocateMemeory(size); // base是分配的内存地址
unsafe.setMemory(base,size,(byte)0);
5.2.2 释放内存
unsafe.freeMemory(base);// 通过base地址释放内存
5.2.3 分配和释放原理
-
使用了Unsafe对象完成直接内存的分配回收,并且回收需要主动调用freeMemory方法
-
ByteBuffer的实现内部类,使用了Cleaner(虚引用)来监测ByteBuffer对象,一旦ByteBufffer对象被垃圾回收,那么就会由ReferenceHandler线程通过Cleaner的clean方法调用freeMemory来释放直接内存