Consuming stack traces noticeably slower in Java 11 than Java 8

前端 未结 2 2011
攒了一身酷
攒了一身酷 2020-12-24 06:33

I was comparing the performance of JDK 8 and 11 using jmh 1.21 when I ran across some surprising numbers:

Java versi         


        
2条回答
  •  借酒劲吻你
    2020-12-24 07:09

    I investigated the issue with async-profiler which can draw cool flame graphs demonstrating where the CPU time is spent.

    As @AlekseyShipilev pointed out, the slowdown between JDK 8 and JDK 9 is mainly the result of StackWalker changes. Also G1 has become the default GC since JDK 9. If we explicitly set -XX:+UseParallelGC (default in JDK 8), the scores will be slightly better.

    But the most interesting part is the slowdown in JDK 11.
    Here is what async-profiler shows (clickable SVG).

    The main difference between two profiles is in the size of java_lang_Throwable::get_stack_trace_elements block, which is dominated by StringTable::intern. Apparently StringTable::intern takes much longer on JDK 11.

    Let's zoom in:

    Note that StringTable::intern in JDK 11 calls do_intern which in turn allocates a new java.lang.String object. Looks suspicious. Nothing of this kind is seen in JDK 10 profile. Time to look in the source code.

    stringTable.cpp (JDK 11)

    oop StringTable::intern(Handle string_or_null_h, jchar* name, int len, TRAPS) {
      // shared table always uses java_lang_String::hash_code
      unsigned int hash = java_lang_String::hash_code(name, len);
      oop found_string = StringTable::the_table()->lookup_shared(name, len, hash);
      if (found_string != NULL) {
        return found_string;
      }
      if (StringTable::_alt_hash) {
        hash = hash_string(name, len, true);
      }
      return StringTable::the_table()->do_intern(string_or_null_h, name, len,
                                           |     hash, CHECK_NULL);
    }                                      |
                           ----------------
                          |
                          v
    oop StringTable::do_intern(Handle string_or_null_h, const jchar* name,
                               int len, uintx hash, TRAPS) {
      HandleMark hm(THREAD);  // cleanup strings created
      Handle string_h;
    
      if (!string_or_null_h.is_null()) {
        string_h = string_or_null_h;
      } else {
        string_h = java_lang_String::create_from_unicode(name, len, CHECK_NULL);
      }
    

    The function in JDK 11 first looks for a string in the shared StringTable, does not find it, then goes to do_intern and immediately creates a new String object.

    In JDK 10 sources after a call to lookup_shared there was an additional lookup in the main table which returned the existing string without creation of a new object:

      found_string = the_table()->lookup_in_main_table(index, name, len, hashValue);
    

    This refactoring was a result of JDK-8195097 "Make it possible to process StringTable outside safepoint".

    TL;DR While interning method names in JDK 11, HotSpot creates redundant String objects. This has happened after JDK-8195097.

提交回复
热议问题