Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use annotations instead of enumerations in Android to save system memory overhead

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use annotations instead of enumerations in Android to save system memory overhead, the article is very detailed, has a certain reference value, interested friends must read it!

Java5 later began to support enumerated types. Enumerated types are very convenient to use, and their important role is to be used as type safety. It won't be a problem if you use a lot of enumerations without considering the system memory overhead. But the mobile side still needs to pay attention.

After the application starts, the android system allocates a separate piece of memory to the application. The application's dex code, heap, and runtime memory allocation are all in this block of memory. Dex size that uses enumerated types is more than 13 times brighter than normal. An enum declaration consumes at least 20 bytes for memory allocation at run time. From these two points, it can be shown that the memory overhead of using enumerations extensively in app is very large.

The biggest advantage of enumerations is type safety. So is there another way to be type-safe without using enumerations? The answer is yes, let's see how to do it.

Goodle officials have long discovered the performance overhead of enumerations on android systems, so developers have been reminded on their website to use enumerations as little as possible, and annotations are also provided to check type safety. At present, it provides two annotation methods: int and string. They are IntDef and StringDef.

Example:

1. Int type annotations check type safety

Package com.yw.enumproxylib;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import androidx.annotation.IntDef;/** * define an annotation of type int * use annotations instead of enumerations to make it type safe * create by yangwei * on 2020-01-30 21:34 * / public class EnumProxy {public static final int READ_CAR = 0 public static final int YELLOW_CAR / red car public static final int YELLOW_CAR = 1 / / Yellow car @ IntDef ({READ_CAR, GREEN_CAR, YELLOW_CAR}) @ Retention (RetentionPolicy.SOURCE) / / Source level public @ interface CAR_Enum {} / * get 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;}

II. String type annotations check type safety

Package com.yw.enumproxylib;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import androidx.annotation.StringDef;/** * defines an annotation of type string 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 defined here can only be these @ Retention (RetentionPolicy.SOURCE) / / compile-time annotations. The source-level check public @ interface Week {} / * get date * * @ param week * @ return * / public static String getWeek (@ Week String week) {switch (week) {case MONDAY: return "Monday"; case TUESDAY: return "Tuesday" Case WENDESDAY: return "Wednesday"; case THURSDAY: return "Thursday"; case FRIDAY: return "Friday"; case SATURDAY: return "Saturday"; case WEEKDAY: return "Sunday";} return null;}}

Third, the MainActivity used to check type safety, you can see below that when using EnumProxy.getCar input parameters, if you enter an integer directly, the compilation will not pass. The compilation passes when you enter the EnumProxy.GREEN_CAR that we define.

In the same way, 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 int ck = EnumProxy.getCar (EnumProxy.GREEN_CAR) here; / / Type check passes String str = "friday"; String week1 = StringEnum.getWeek (str); / / Type check fails String week2 = StringEnum.getWeek (StringEnum.FRIDAY) when assigning String directly; / / Type check passes}} when assigning defined values.

These are all the contents of the article "how to use annotations instead of enumerations in Android to save system memory overhead". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report