字符串常量池与堆空间
运行后 为了不会被回收空间,sleep一小时
jps命令查询Java程序
使用jamp查询堆空间
jmap -dump:format=b,file=aa.bin(aa.bin=随意名字) pid(pid=Java线程id)
jhat aa.bin
访问 http://localhost:7000/
我是访问的是http://localhost:7000/oql/
OQL Java虚拟机语言 查询字符串地址
select s.value.toString() from java.lang.String s
where /kjlhgfc/.test(s.value.toString())
kjlhgfc 查询结果只有一个 说明只在堆中存在,而字符串常量池中保存的是引用地址
gfdss 查询结果有两个 说明只在堆中存在,而字符串常量池中保存的是也是字符串,所以地址不一样
在使用"kjlhgf" 时 编译期就会存入常量池,也会在堆中放入,但是在append后就被修改为kjlhgfc,所以查询结果只剩下一个。
lsof -i:7000
查询7000端口使用者
intern方法的解释
/**
* Returns a canonical representation for the string object.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
* <p>
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
* <p>
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@code true}.
* <p>
* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
* <cite>The Java™ Language Specification</cite>.
*
* @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern();