Go Android High Performance Programming (2)--deferred initialization

Source: Internet
Author: User

In the previous article, there are many performance and memory tips that need to be noted in the development of Android apps. This article is an analysis of performance optimization and memory optimization issues from the Smali instruction level.

How to solve the problem of high interface start time overhead

When we write Android applications, there are many situations where the interface is too long to start and the user experience is very bad. So when we write code, we must pay more attention to how to improve the start time of the interface. Here are a few tips on how to optimize the interface startup overhead.

1. Load overhead for classes

When a static method or static property of a class is called or the class is instantiated, the first thing the virtual machine does is dexclassloader the class file to the virtual machine, and the process of loading into the virtual machine triggers the execution of the Clinit function in the class file. Let's look at the following section of code

[CPP]View Plaincopy
  1. Package Com.example.smalidemo.foreach;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Public class Oninittest {
  5. public static final String init_string = "Initstring_fantasy";
  6. public static String init2_string = "for Initstring_fantasy 2";
  7. private String init3_string = "Initstring_fantasyh 3";
  8. public static final int int_1 = 100;
  9. public static int init_int = 10000;
  10. public static List List = new ArrayList ();
  11. public static final arraylist<appbean> mapplistoninit = new arraylist<appbean> ();
  12. public static ArrayList mapplist = null;
  13. private static String string_array[] = { "jpg", "MP5", "mp4"};
  14. private static final String final_string_array[] = { "pdf", "txt",
  15. "EXE"};
[CPP]View Plaincopy
    1. }




Post-compilation Smali files

[HTML]View Plaincopy
  1. . class public lcom/example/smalidemo/foreach/oninittest;
  2. . Super Ljava/lang/object;
  3. . Source "Oninittest.java"
  4. # static fields
  5. . field private static final final_string_array:[ljava/lang/string; = null
  6. . field public static init2_string:ljava/lang/string; = NULL
  7. . field public static init_int:i = 0x0
  8. . field public static final init_string:ljava/lang/string; = "Initstring_fantasy"
  9. . field public static final int_1:i = 0x64
  10. . field private static string_array:[ljava/lang/string;
  11. . field public static list:ljava/util/list;
  12. . field public static mapplist:ljava/util/arraylist;
  13. . field public static final mapplistoninit:ljava/util/arraylist;
  14. . annotation system ldalvik/annotation/signature;
  15. value = {
  16. "Ljava/util/arraylist",
  17. "<",
  18. "Lcom/example/smalidemo/foreach/appbean;",
  19. ">;"
  20. }
  21. . End annotation
  22. . End Field
  23. # instance Fields
  24. . field private init3_string:ljava/lang/string;
  25. # Direct methods
  26. . method Static constructor <clinit> () V
  27. . Locals 10
  28. . Prologue
  29. CONST/16 V9, 0x2710
  30. CONST/4 V8, 0x3
  31. CONST/4 V7, 0x2
  32. CONST/4 V6, 0x1
  33. CONST/4 V5, 0x0
  34. . Line 8
  35. Const-string v3, "for Initstring_fantasy 2"
  36. Sput-object v3, lcom/example/smalidemo/foreach/oninittest;->INIT2_STRING:Ljava/lang/String;
  37. . Line 11
  38. Sput v9, lcom/example/smalidemo/foreach/oninittest;->init_int:i
  39. . Line 12
  40. New-instance v3, ljava/util/arraylist;
  41. Invoke-direct {v3}, ljava/util/arraylist;-><init> () V
  42. Sput-object v3, lcom/example/smalidemo/foreach/oninittest;->list:Ljava/util/List;
  43. . Line 13
  44. New-instance v3, ljava/util/arraylist;
  45. Invoke-direct {v3}, ljava/util/arraylist;-><init> () V
  46. Sput-object v3, lcom/example/smalidemo/foreach/oninittest;->mAppListOnInit:Ljava/util/ArrayList;
  47. . Line 14
  48. CONST/4 v3, 0x0
  49. Sput-object v3, lcom/example/smalidemo/foreach/oninittest;->mAppList:Ljava/util/ArrayList;
  50. . Line 15
  51. New-array v3, V8, [ljava/lang/string;
  52. Const-string v4, "JPG"
  53. Aput-object v4, v3, v5
  54. Const-string v4, "MP5"
  55. Aput-object v4, V3, V6
  56. Const-string v4, "MP4"
  57. Aput-object v4, v3, V7
  58. Sput-object v3, lcom/example/smalidemo/foreach/oninittest;->STRING_ARRAY:[Ljava/lang/String;
  59. . line 16
  60. New-array v3, V8, [ljava/lang/string;
  61. Const-string v4, "PDF"
  62. Aput-object v4, v3, v5
  63. Const-string v4, "TXT"
  64. Aput-object v4, V3, V6
  65. . Line 17
  66. Const-string v4, "EXE"
  67. Aput-object v4, v3, V7
  68. . line 16
  69. Sput-object v3, lcom/example/smalidemo/foreach/oninittest;->FINAL_STRING_ARRAY:[Ljava/lang/String;
  70. . Line 20
  71. CONST/4 v1, 0x0
  72. . Local V1, i:i
  73. Move v2, V1
  74. . Line 21
  75. . End Local V1 #i: I
  76. . Local v2, I:i
  77. : goto_0
  78. ADD-INT/LIT8 v1, v2, 0x1
  79. . End Local V2 #i: I
  80. . Restart Local V1 #i: I
  81. IF-LT v2, V9,: cond_0
  82. . Line 25
  83. Return-void
  84. . Line 22
  85. : Cond_0
  86. New-instance V0, Lcom/example/smalidemo/foreach/appbean;
  87. Invoke-direct {v0}, lcom/example/smalidemo/foreach/appbean;-><init> () V
  88. . line 23
  89. . local V0, Bean:lcom/example/smalidemo/foreach/appbean;
  90. Sget-object v3, lcom/example/smalidemo/foreach/oninittest;->mAppListOnInit:Ljava/util/ArrayList;
  91. invoke-virtual {v3, v0}, ljava/util/arraylist;->add (ljava/lang/object;) Z
  92. Move v2, V1
  93. . End Local V1 #i: I
  94. . Restart Local v2 #i: I
  95. Goto:goto_0
  96. . End method

In the Java file above, we declare a number of variables, including strings, integers, arrays of strings, some of which declare final, and some do not declare final. We can see that the final string and integer are declared, the compiled file has become a constant, and no final variable is declared, and we can see that the default value of string, which is still the type default value, is null at the point where it is declared, and the default value of int is 0. So when Dexclassloader loads the class file, it will initialize the values for the above static variables in the <clinit> function. We can see that a number of static variables are declared, in the compiled Smali file, become more Smali instructions.

Increasing the speed of the Dexclassloader loading class is to improve the execution speed of the <clinit> function in class.

Through the above analysis we can summarize the following rules:

Declare a static variable, be sure to add a final declaration (the compiler variable is replaced by the constant, it will not load the CPU time when the class loaded)

2. Creating instance overhead for classes

In addition to static variables in a class file, there are many global non-static variables. When we declare global variables, we assign values to global variables.

[CPP]View Plaincopy
    1. Private String init3_string = "Initstring_fantasyh 3";


This Java statement above will become a few instructions in the Smali file. Let's take a look at:

[HTML]View Plaincopy
    1. # instance Fields
    2. . field private init3_string:ljava/lang/string;

[HTML]View Plaincopy
  1. . method Public constructor <init> () V
  2. . Locals 1
  3. . Prologue
  4. . Line 6
  5. Invoke-direct {p0}, ljava/lang/object;-><init> () V
  6. . Line 9
  7. Const-string V0, "INITSTRING_FANTASYH 3"
  8. Iput-object V0, P0, lcom/example/smalidemo/foreach/oninittest;->INIT3_STRING:Ljava/lang/String;
  9. . Line 6
  10. Return-void
  11. . End method


As you can see, in the <init> function, the global non-static variables are initialized. Class file is the process of executing the <init> function when creating instances of classes.

The process of improving the class creation instance is the process of optimizing <init> function execution speed. Our best practice is to assign a default value where the global variable is declared, and then initialize it when it is actually used in the function.

Whether we are static global variables or non-static global variables, these variables are assigned during class loading and instantiation. If we assign a value at the time of the declaration, we assign a value to the INIT function, or else an assignment of NULL or 0 is executed. Therefore, it is a great rule to declare global variables as few as possible. (because whenever a global variable is declared, an instruction is executed when the class is loaded or initialized.) )

The above two points are optimized, I believe that the display speed of the interface to improve a lot.

But when we need to define a set or array of constants in our code, how can we avoid the overhead of these two processes? For example, in development, we need to define a static global array. If defined in a class that is instantiated in the activity's oncreate, it will certainly consume a lot of activity startup time. How do you optimize for this problem? We can refactor this static array into another class, and we can call the static array in this class directly when we use the array. This avoids the problem of initializing the majority of groups in the activity initialization process.

Lazy initialization is a powerful technique for optimizing activity startup time. On the basis of not modifying the algorithm and the logical structure, the delay initialization can also achieve a certain degree of optimization.

Through the above analysis can summarize the following several rules:

1. Declare global variables as few as possible

2. Declaring a static variable must be a final statement

3. For large overhead static modules or global non-static modules, can be refactored into another class, to achieve the role of lazy initialization.

Excerpt from: http://blog.csdn.net/litton_van/article/details/21872677

Go Android High Performance Programming (2)--deferred initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.