devxlogo

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

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

String in Java is a constant object, which means by default it can becreated and read but not modified. Usually developers use the Stringextensively but forget the overhead it may cause for the application.Every time you connect two string objects, JVM creates a new StringObject for that and frees the previous ones. So, for example, if we considerthe following function :

 public class stringClass {  public String getTabSeparatedData(String[] data) {    String newData = "";    for (int i = 0;i 

for a set of 100 data, The VM has to recreate the string newData 100 timesand also have to submit the left behind memory to garbage collector toidentify 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 significantfactor.

 public class stringClass {  public String getTabSeparatedData(String[] data) {    StringBuffer newData = new StringBuffer();    for (int i = 0;i 

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 garbagecollector. 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    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   15 aload_1  16 iload_3  17 aaload  18 invokestatic #4   21 ldc #5   23 invokevirtual #6 "creating a new String object!!!!"  26 invokestatic #4   29 invokevirtual #6 "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 getseliminated by using StringBuffer in place of String:

 Method java.lang.String getTabSeparatedData(java.lang.String[])   0 new #2    3 dup   4 invokespecial #3    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   "Just expand the memory to allocate new space"  23 ldc #5   25 invokevirtual #4  "Just expand the memory to allocate new space"  28 pop  29 iinc 3 1  32 goto 10  35 aload_2  36 invokevirtual #6   39 areturn
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist