Jackson triggered the String. intern () bug, resulting in a continuous increase in memory, JVM-Java Memory leakage,

Source: Internet
Author: User

Jackson triggered the String. intern () bug, resulting in a continuous increase in memory, JVM-Java Memory leakage,

I can use Jackson locally to reproduce this problem.

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);     }   } } 

This is the code I reproduced. Every time I generate a random integer as the map key, I use objectMapper for deserialization. Then I run another PrintStringTable class, and we can see that each Integer generated enters the Constant Pool.
If I change the code for constructing ObjectMapper to the code I commented out, no matter how many random Integer keys are generated, they will not enter the Constant Pool.
The PrintStringTable class is as follows:

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());         }     } } 

 

The/{jdk_HOME}/lib/sa-jdi.jar needs to be compiled and run. The running parameter is the pid of attach.

 

Key points for Modification

protected JsonFactory factory = new JsonFactory().disable(JsonFactory.Feature.INTERN_FIELD_NAMES);protected ObjectMapper mapper = new ObjectMapper(factory).setTimeZone(TimeZone.getDefault());

 

 

See Oracle Java official documentation

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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.