我在本地用Jackson可以復現這個問題了。 這是我復現的代碼,我每次產生一個隨機的integer作為map的key,然後用objectMapper反序列化。然後我運行我的另外一個PrintStringTable的類,可以看到每次產生的Integer都會進入Constant Pool中如果我把構造O ...
我在本地用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)