Android uses annotations instead of enumeration to save system memory overhead

  • 2021-11-14 07:11:52
  • OfStack

Since Java5, enumerated types have been supported. Enumerated types are very convenient to use, and their important role is to use them as type safety. If you use enumerations in large quantities without considering the system memory overhead, there will be no problem. But the mobile terminal still needs attention.

android system will allocate 1 block of memory to the application after the application starts. The dex, code, heap of the application and the memory allocation at runtime will all be in this memory. While dex size with enumeration type is more than 13 times as bright as normal. At runtime memory allocation, a declaration of an enum consumes at least 20 bytes. From these two points, it can be seen that the memory overhead of using enumerations extensively in app is very large.

The greatest advantage of enumeration is type safety. So is it possible to achieve type safety without enumeration? The answer is yes. Let's see how to do it.

goodle officials have long discovered the performance overhead brought by enumeration to android system, so they have reminded developers to use enumeration as little as possible on their official website, and also provided annotations to check type safety. At present, it provides int and string annotation methods. They are: IntDef and StringDef.

Don't say much, just give an example. ps: There are detailed comments in demo.

1. Int type annotations check for type safety


package com.yw.enumproxylib;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import androidx.annotation.IntDef;

/**
 *  Definition 1 A int Annotation of type 
 *  Use annotations instead of enumeration to achieve type safety 
 * create by yangwei
 * on 2020-01-30 21:34
 */
public class EnumProxy {
  public static final int READ_CAR = 0;// A red car 
  public static final int GREEN_CAR = 1;// A green car 
  public static final int YELLOW_CAR = 2;// A yellow car 


  @IntDef({READ_CAR, GREEN_CAR, YELLOW_CAR})
  @Retention(RetentionPolicy.SOURCE)// Source code level 
  public @interface CAR_Enum {

  }

  /**
   *  Get the car type 
   *
   * @param car
   * @return
   */
  public static int getCar(@CAR_Enum int car) {
    switch (car) {
      case READ_CAR:
        return 0;
      case GREEN_CAR:
        return 1;
      case YELLOW_CAR:
        return 2;
    }
    return -1;
  }
}

2. String type annotations check type safety


package com.yw.enumproxylib;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import androidx.annotation.StringDef;

/**
 *  Definition 1 A string Type, which is used to check the type safety of the week. 
 * create by yangwei
 * on 2020-01-30 22:04 week
 */
public class StringEnum {

  public static final String MONDAY = "monday";
  public static final String TUESDAY = "tuesday";
  public static final String WENDESDAY = "wendesday";
  public static final String THURSDAY = "thursday";
  public static final String FRIDAY = "friday";
  public static final String SATURDAY = "saturday";
  public static final String WEEKDAY = "weekday";

  @StringDef({MONDAY, TUESDAY, WENDESDAY, THURSDAY, FRIDAY, SATURDAY, WEEKDAY})// The values of annotations defined here can only be these 
  @Retention(RetentionPolicy.SOURCE)// Compile-time annotations, source-level checks 
  public @interface Week {

  }

  /**
   *  Get Date 
   *
   * @param week
   * @return
   */
  public static String getWeek(@Week String week) {
    switch (week) {
      case MONDAY:
        return " Week 1";
      case TUESDAY:
        return " Week 2";
      case WENDESDAY:
        return " Week 3";
      case THURSDAY:
        return " Week 4";
      case FRIDAY:
        return " Week 5";
      case SATURDAY:
        return " Week 6";
      case WEEKDAY:
        return " Sunday ";
    }
    return null;
  }
}

3. MainActivity used to check type safety. As you can see from the following, when using EnumProxy. getCar input parameters, if you directly enter 1 integer, the compilation will not pass. Compilation passes only when you enter our defined EnumProxy. GREEN_CAR.

Similarly, StringEnum can also check type safety.


package com.yw.enumproxy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.yw.enumproxylib.EnumProxy;
import com.yw.enumproxylib.StringEnum;

/**
 *  Test example 
 */
public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int car_black = 10;
    int bk = EnumProxy.getCar(car_black);// Type check fails here 
    int ck = EnumProxy.getCar(EnumProxy.GREEN_CAR);// Type checking passes 


    String str = "friday";
    String week1 = StringEnum.getWeek(str);// Direct assignment String Type check fails when 
    String week2 = StringEnum.getWeek(StringEnum.FRIDAY);// Type checking passes when assigning a defined value 
  }
}

If you have any questions about the above knowledge points, you can contact this site to supplement and share it.


Related articles: