案例分析:java中substring引发的Full gc

问题定位

由于应用频繁地Full gc,就dump了内存下来用MAT分析,发现有个map占用了98%的内存,于是找到这个map

private ConcurrentMap<String, String> nick2numid = new ConcurrentHashMap<String, String>();

存放的是nick与id的映射关系,从MAT中找到map的每一个entry如下图所示:


这里解释一下两个概念

Shallow Heap:对象占用了多少内存(单位:字节)

Retained Heap:如果对象被回收会释放多少内存,也就是对象hold住的内存

map的key是一个String类型,其Shallow Heap为32byte,Retained Heap为1104byte。一般对于String类型,具有不可变性,这两个值应该相等才对,带着疑惑找到了问题所在,分步描述如下:

1、从页面传了一个参数到后端,这个参数携带了cookie的内容,恰好就是1104byte这么长,不妨设置这个参数为cookie;

2、后端拿到cookie这个参数后,需要其中的nick的值,采用的是String类的split方法(&做为分隔符)得到一个数组,其中有一项的值为nick=xxx

3、将nick的值做为key放入nick2numid中

nick2numid.put("xxx",id)

最终发现问题出在String类的substring方法上?

分析问题

其实String的split方法上调用了substring方法,先来看看split的源码实现吧

public String[] split(String regex) {
    return split(regex, 0);
}
public String[] split(String regex, int limit) {
		return Pattern.compile(regex).split(this, limit);
}

 public String[] split(CharSequence input, int limit) {
        ArrayList<String> matchList = new ArrayList<String>();
        Matcher m = matcher(input);

        // Add segments before each match found
        while(m.find()) {
            if (!matchLimited || matchList.size() < limit - 1) {
                String match = input.subSequence(index, m.start()).toString();
                matchList.add(match);
                index = m.end();
            } else if (matchList.size() == limit - 1) { // last one
                String match = input.subSequence(index,
                                                 input.length()).toString();
                matchList.add(match);
                index = m.end();
            }
        }
}   

public CharSequence subSequence(int beginIndex, int endIndex) {
    return this.substring(beginIndex, endIndex);
}

String.split->Pattern.split->subSequence->substring

从以上代码可以看出String类的split方法确实调用了substring方法
下面来看看substring方法源码:

private final char value[];

public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
	throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
	throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
	throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
	new String(offset + beginIndex, endIndex - beginIndex, value);
}

注意value这个char数组,存放的是String每个字符的内容,substring直接依赖了这个数组。如果substring产生的字符串没有被java虚拟机回收,这个char数组也不会被回收。

问题回顾:应用中的nick2numId这个map生命周期很长,只有在用户退出的时候才会删除其中的entry项,恰好这个map非常大,dump内存的时候size已经达到几十万,相当于几十万个cookie被这个map所hold住,内存被耗尽产生Full gc。

解决方案

当时想到几种方案:
1、String.intern()?
2、拿到value数组产生一个新数组?
3、采用分布式缓存来存放nick与id的映射关系?

采用第一种方法是否可行?不可取,原因有2:

intern() 所使用的是一个全局的池,并不需要如此大作用域的缓存;

intern会向常量池中添加内容,持久代空间本来就很小,被nick所占用可能引起OutOfMemoryError!

后来采用的是第二种方法:new String(nick.toCharArray()),很快上线使内存利用率得到提升(fast,not the best)。

JVM中存放生命周期长的大map始终是一个隐患,说不定哪一天由于map元素过多或者删除不及时导致OOM,应尽快采用第3种方案:分布式缓存。nick放入缓存之后,cookie会很快被回收,而且系统的可用内存将会大大提高。

原文地址:https://www.cnblogs.com/dyllove98/p/3202817.html