Dismiss
InnovationQ will be updated on Sunday, Oct. 22, from 10am ET - noon. You may experience brief service interruptions during that time.
Browse Prior Art Database

A Method to Reduce the Reallocation of StringBuffer in Java

IP.com Disclosure Number: IPCOM000029110D
Original Publication Date: 2004-Jun-16
Included in the Prior Art Database: 2004-Jun-16
Document File: 1 page(s) / 7K

Publishing Venue

IBM

Abstract

Disclosed is a method to reduce the reallocation of StringBuffer's internal buffer in Java.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 100% of the total text.

Page 1 of 1

A Method to Reduce the Reallocation of StringBuffer in Java

Disclosed is a method to reduce the reallocation of StringBuffer's internal buffer in Java.

In Java programming language, StringBuffer objects are used to manipulate strings such as their concatenation. However, the initial size of StringBuffer's internal buffer is defined as only 16 characters. Therefore, its reallocation occurs very frequently, which causes frequent GC invocation and unnecessary data copies.

When a StringBuffer object is newly created, the disclosed method allocates its internal buffer longer than 16 characters if:
(a) The internal buffer is allocated on the creator thread's stack instead of global heap area, and
(b) This implicit size increment is unobservable from outside.

The condition (a) is satisfied if the StringBuffer's internal buffer is accessible only by its creator thread. This can be determined by escape analysis of JIT compiler. In many cases, StringBuffer objects are used just as temporary work area for string manipulation, so are not escaping.

The condition (b) is satisfied if "capacity()" method is not invoked for the StringBuffer object. This can also be determined during the escape analysis.

1