Uploaded image for project: 'Hive'
  1. Hive
  2. HIVE-19668

Over 30% of the heap wasted by duplicate org.antlr.runtime.CommonToken's and duplicate strings

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.0.0
    • Fix Version/s: 4.0.0, 3.2.0
    • Component/s: HiveServer2
    • Labels:
      None

      Description

      I've recently analyzed a HS2 heap dump, obtained when there was a huge memory spike during compilation of some big query. The analysis was done with jxray (www.jxray.com). It turns out that more than 90% of the 20G heap was used by data structures associated with query parsing (org.apache.hadoop.hive.ql.parse.QBExpr). There are probably multiple opportunities for optimizations here. One of them is to stop the code from creating duplicate instances of org.antlr.runtime.CommonToken class. See a sample of these objects in the attached image:

      Looks like these particular CommonToken objects are constants, that don't change once created. I see some code, e.g. in org.apache.hadoop.hive.ql.parse.CalcitePlanner, where such objects are apparently repeatedly created with e.g. new CommonToken(HiveParser.TOK_INSERT, "TOK_INSERT") If these 33 token kinds are instead created once and reused, we will save more than 1/10th of the heap in this scenario. Plus, since these objects are small but very numerous, getting rid of them will remove a gread deal of pressure from the GC.

      Another source of waste are duplicate strings, that collectively waste 26.1% of memory. Some of them come from CommonToken objects that have the same text (i.e. for multiple CommonToken objects the contents of their 'text' Strings are the same, but each has its own copy of that String). Other duplicate strings come from other sources, that are easy enough to fix by adding String.intern() calls.

        Attachments

        1. HIVE-19668.01.patch
          19 kB
          Misha Dmitriev
        2. HIVE-19668.02.patch
          19 kB
          Misha Dmitriev
        3. HIVE-19668.03.patch
          20 kB
          Misha Dmitriev
        4. HIVE-19668.04.patch
          20 kB
          Misha Dmitriev
        5. HIVE-19668.05.patch
          20 kB
          Misha Dmitriev
        6. image-2018-05-22-17-41-39-572.png
          377 kB
          Misha Dmitriev

          Activity

            People

            • Assignee:
              misha@cloudera.com Misha Dmitriev
              Reporter:
              misha@cloudera.com Misha Dmitriev
            • Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: