Jackson 触发的String.intern() bug, 导致内存持续增加,JVM-Java内存泄漏
2017-09-04 15:33 halberts 阅读(4092) 评论(0) 收藏 举报我在本地用Jackson可以复现这个问题了。
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws IOException {
// JsonFactory factory = new JsonFactory().disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
// ObjectMapper om = new ObjectMapper(factory);
ObjectMapper om = new ObjectMapper();
Random random = new Random();
while (true) {
System.out.println("Press any key to continue");
System.in.read();
int key = random.nextInt();
System.out.println("Generated key: " + key);
Map<Integer, Boolean> desMap =
om.readValue(String.format("{\"%s\":\"true\"}", key), new TypeReference<Map<Integer, Boolean>>() {});
System.out.println("Read map: " + desMap);
}
}
}
这是我复现的代码,我每次产生一个随机的integer作为map的key,然后用objectMapper反序列化。然后我运行我的另外一个PrintStringTable的类,可以看到每次产生的Integer都会进入Constant Pool中
如果我把构造ObjectMapper的代码改成我注释掉的代码的话,不管产生多少随机Integer key,都不会进入Constant Pool
PrintStringTable类如下
import sun.jvm.hotspot.memory.StringTable;
import sun.jvm.hotspot.memory.SystemDictionary;
import sun.jvm.hotspot.oops.Instance;
import sun.jvm.hotspot.oops.InstanceKlass;
import sun.jvm.hotspot.oops.OopField;
import sun.jvm.hotspot.oops.TypeArray;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
public class PrintStringTable extends Tool {
public PrintStringTable() {
}
public static void main(String args[]) throws Exception {
if (args.length == 0 || args.length > 1) {
System.err.println("Usage: java PrintStringTable <PID of the JVM whose string table you want to print>");
System.exit(1);
}
PrintStringTable pst = new PrintStringTable();
pst.execute(args);
pst.stop();
}
@Override
public void run() {
StringTable table = VM.getVM().getStringTable();
table.stringsDo(new StringPrinter());
}
class StringPrinter implements StringTable.StringVisitor {
private final OopField stringValueField;
public StringPrinter() {
InstanceKlass strKlass = SystemDictionary.getStringKlass();
stringValueField = (OopField) strKlass.findField("value", "[C");
}
@Override
public void visit(Instance instance) {
TypeArray charArray = ((TypeArray) stringValueField.getValue(instance));
StringBuilder sb = new StringBuilder();
for (long i = 0; i < charArray.getLength(); i++) {
sb.append(charArray.getCharAt(i));
}
System.out.println("Address: " + instance.getHandle() + " Content: " + sb.toString());
}
}
}
需要用/{jdk_HOME}/lib/sa-jdi.jar进行编译和运行,运行参数是需要attach的pid
修改要点
protected JsonFactory factory = new JsonFactory().disable(JsonFactory.Feature.INTERN_FIELD_NAMES); protected ObjectMapper mapper = new ObjectMapper(factory).setTimeZone(TimeZone.getDefault());
参考Oracle Java 官方文档
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/nmt-8.html
https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr007.html (Native Memory Tracking)
浙公网安备 33010602011771号