Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Sep 5, 2000

Don't forget that String may cause overhead for your app

String in Java is a constant object, which means by default it can be created and read but not modified. Usually developers use the String extensively but forget the overhead it may cause for the application. Every time you connect two string objects, JVM creates a new String Object for that and frees the previous ones. So, for example, if we consider the following function :
 
public class stringClass {

  public String getTabSeparatedData(String[] data) {
    String newData = "";
    for (int i = 0;i < data.length; i++) {
      newData += data[i] + "\t";
    }

    return newData;
  }
}

for a set of 100 data, The VM has to recreate the string newData 100 times and also have to submit the left behind memory to garbage collector to identify and delete. But if we use StringBuffer in place of the String in above application, which is truly a dynamic object, then the overhead reduces by significant factor.

 
public class stringClass {

  public String getTabSeparatedData(String[] data) {
    StringBuffer newData = new StringBuffer();
    for (int i = 0;i < data.length; i++) {
      newData.append(data[i]).append("\t");
    }

    return newData.toString();
  }
}

Because every time we travel through the loop, we use the method "append()" to modify the memory, rather than creating new memory each time. You will be expanding the memory rather than leaving a mess for the garbage collector. The result is increased performance. This can be understood in much clearer way if we inspect the byte code generated by the compiler for the above two classes: Byte code for the first example, shows how the memory overhead was created:

 
Method java.lang.String getTabSeparatedData(java.lang.String[])
   0 ldc #3 <String "">
   2 astore_2
   3 iconst_0
   4 istore_3
   5 iload_3
   6 aload_1
   7 arraylength
   8 if_icmpge 39
  11 aload_2
  12 invokestatic #4 <Method java.lang.String valueOf(java.lang.Object)>
  15 aload_1
  16 iload_3
  17 aaload
  18 invokestatic #4 <Method java.lang.String valueOf(java.lang.Object)>
  21 ldc #5 <String "   ">
  23 invokevirtual #6 <Method java.lang.String concat(java.lang.String)>
"creating a new String object!!!!"
  26 invokestatic #4 <Method java.lang.String valueOf(java.lang.Object)>
  29 invokevirtual #6 <Method java.lang.String concat(java.lang.String)>
"Creating a new String object!!!"
  32 astore_2
  33 iinc 3 1
  36 goto 5
  39 aload_2
  40 areturn

Byte code for the second example, showing how the memory overhead gets eliminated by using StringBuffer in place of String:

 
Method java.lang.String getTabSeparatedData(java.lang.String[])
   0 new #2 <Class java.lang.StringBuffer>
   3 dup
   4 invokespecial #3 <Method java.lang.StringBuffer()>
   7 astore_2
   8 iconst_0
   9 istore_3
  10 iload_3
  11 aload_1
  12 arraylength
  13 if_icmpge 35
  16 aload_2
  17 aload_1
  18 iload_3
  19 aaload
  20 invokevirtual #4 <Method java.lang.StringBuffer
append(java.lang.String)>  "Just expand the memory to allocate new space"
  23 ldc #5 <String "   ">
  25 invokevirtual #4 <Method java.lang.StringBuffer
append(java.lang.String)> "Just expand the memory to allocate new space"
  28 pop
  29 iinc 3 1
  32 goto 10
  35 aload_2
  36 invokevirtual #6 <Method java.lang.String toString()>
  39 areturn
Ayan Bandopadhyay
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date