Uploaded image for project: 'Ignite'
  1. Ignite
  2. IGNITE-11911

GridToStringBuilder allocates redundant memory

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.7
    • 2.8
    • None
    • None

    Description

      A lot of classes in Ignite uses GridToStringBuilder to implement their toString() method. GridToStringBuilder uses thread local buffer to generate string representation. This buffer extended on demand but never shrank. On the final step GridToStringBuilder uses java StringBuilder class to produce output string and uses own buffer size as StringBuilder initial capacity. This leads to unnecessary memory allocation since we only need to allocate memory for meaningful data, but not for all buffer.

       Reproducer:

       

      public void testSB() {
          GridToStringBuilder.toString(C1.class, new C1(1_000_000));
          GridToStringBuilder.toString(C1.class, new C1(100));
      }
      
      public class C1 {
          private String f;
      
          C1(int len) {
              f = new String(new char[len]);
          }
      }
      

      Here on the second line about 1 million chars StringBuilder will be allocated, but only about 100 chars needed.

       

      Problem code (SBLimitedLength.java:293):

       

      StringBuilder res = new StringBuilder(impl().capacity() + tailLen + 100);
      

      Here length() method can be used instead of capacity()

       

      Attachments

        Issue Links

          Activity

            People

              alex_pl Aleksey Plekhanov
              alex_pl Aleksey Plekhanov
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m