code: public static void trimToMaxSize(final StringBuilder stringBuilder, final int maxSize) {
          if (stringBuilder != null && stringBuilder.capacity() > maxSize) {
              stringBuilder.setLength(maxSize);
              stringBuilder.trimToSize();
          }
      }
docs: Ensures that the char[] array of the specified StringBuilder does not exceed the specified number of characters.
  This method is useful to ensure that excessively long char[] arrays are not kept in memory forever.
  
  @param stringBuilder the StringBuilder to check
  @param maxSize the maximum number of characters the StringBuilder is allowed to have
  @since 2.9