User Interface | SmartSync | GenericSync | Persistence | Trace | General Programming Tips

 

Performance Recommendations - General Programming

Recommendation

Description
Careful instantiation of objects.

The CrEme virtual machine uses more resources for emory allocation and object instantiation than the Win32 standard Sun VM.

The following example shows an unnecessary instantiation:

// set quantity to sales order item
  if ((quantity != null) && !("".equals(quantity)))            {
    salesOrderItem.setReqQty(new FixedDecimal(quantity, 14, 3));
  } else {
    salesOrderItem.setReqQty(new FixedDecimal("0", 14, 3));
  }

Reason:

If the quantity value is not set, a constant quantity of 0 (represented as a FixedDecimal) will be assigned. The FixedDecimal object is not defined as a constant so it will be instantiated every time. This is resource consuming, especially with a FixedDecimal object.

Solution:

Define constant values as constants (final static) and try to instantiate objects once and reuse them as often as possible. However, excessive saving of objects can have a negative affect on the memory consumption.

Use StringBuffer instead of string concatenation.

Concatenation of more than 2 Strings with the + Operator in frequently called methods is very resource consuming. This is a general Java issue. When concatenating Strings with + a new String Object is instantiated, together with memory-allocation for the underlying char array, for every single concatenation.

Example: Following command instantiates 9 objects and allocates char-arrays.

String s = “hello ” + “world ” + “here ” + “I “ + “am “; 

Example: Ineffective way to a add trailing spaces.

private String addLeadingSpaces(String st, int len)
{
  while (st.length() < len)
  {
    st = "0"+ st;
  }
  return st;
}