garbage collection - Does chaining methods vs making temporary variables in Java impact memory allocation? -
in java, if i'm performing multiple methods on object, can chain them, or can make temporary variable, so
chaining
system.out.println( str.substring(0,4).substring(0,2)); temp variable
string tmp = str.substring(0,4); tmp = tmp.substring(0,2); system.out.println(tmp); obviously, difference negligible in example, make impact when you're doing on thousands of strings/some other object.
my question is, 1 of these more "efficient" in terms of not making object allocations or filling heap (and making gc called sooner)?
i tried compare bytecodes of 2 in loop on couple strings, looks similar, sans last few lines. don't understand bytecode calls, i'm not sure if of these have allocating new objects.
compiled "testnotmp.java" public class testnotmp { public testnotmp(); code: 0: aload_0 1: invokespecial #1 // method java/lang/object."<init>":()v 4: return public static void main(java.lang.string[]); code: 0: iconst_4 1: anewarray #2 // class java/lang/string 4: dup 5: iconst_0 6: ldc #3 // string these 8: aastore 9: dup 10: iconst_1 11: ldc #4 // string are__ 13: aastore 14: dup 15: iconst_2 16: ldc #5 // string some_ 18: aastore 19: dup 20: iconst_3 21: ldc #6 // string strings 23: aastore 24: astore_1 25: aload_1 26: astore_2 27: aload_2 28: arraylength 29: istore_3 30: iconst_0 31: istore 4 33: iload 4 35: iload_3 36: if_icmpge 69 39: aload_2 40: iload 4 42: aaload 43: astore 5 45: getstatic #7 // field java/lang/system.out:ljava/io/printstream; 48: aload 5 50: iconst_0 51: iconst_4 52: invokevirtual #8 // method java/lang/string.substring:(ii)ljava/lang/string; 55: iconst_0 56: iconst_2 57: invokevirtual #8 // method java/lang/string.substring:(ii)ljava/lang/string; 60: invokevirtual #9 // method java/io/printstream.println:(ljava/lang/string;)v 63: iinc 4, 1 66: goto 33 69: return } public class testtmp { public testtmp(); code: 0: aload_0 1: invokespecial #1 // method java/lang/object."<init>":()v 4: return public static void main(java.lang.string[]); code: 0: iconst_4 1: anewarray #2 // class java/lang/string 4: dup 5: iconst_0 6: ldc #3 // string these 8: aastore 9: dup 10: iconst_1 11: ldc #4 // string are__ 13: aastore 14: dup 15: iconst_2 16: ldc #5 // string some_ 18: aastore 19: dup 20: iconst_3 21: ldc #6 // string strings 23: aastore 24: astore_1 25: aload_1 26: astore_2 27: aload_2 28: arraylength 29: istore_3 30: iconst_0 31: istore 4 33: iload 4 35: iload_3 36: if_icmpge 77 39: aload_2 40: iload 4 42: aaload 43: astore 5 45: aload 5 47: iconst_0 48: iconst_4 49: invokevirtual #7 // method java/lang/string.substring:(ii)ljava/lang/string; 52: astore 6 54: aload 6 56: iconst_0 57: iconst_2 58: invokevirtual #7 // method java/lang/string.substring:(ii)ljava/lang/string; 61: astore 6 63: getstatic #8 // field java/lang/system.out:ljava/io/printstream; 66: aload 6 68: invokevirtual #9 // method java/io/printstream.println:(ljava/lang/string;)v 71: iinc 4, 1 74: goto 33 77: return }
in example, you're working strings, immutable. in code:
str.substring(0,4).substring(0,2) the first call substring must generate new string object because str cannot modified. similarly, second call substring on new string object create new string object.
the difference in bytecodes result of order in compiler calls methods. in testtmp case, string manipulation occurs before call printstream. testnotmp, string calls happen within printstream call, logical when @ code.
to answer question, make no difference in terms of object allocation , therefore gc impact.
Comments
Post a Comment